setTimeout JavaScript

Posted on: July 5th, 2022
By: Tadeo Martinez

With the following code, you can add a delayed action.

setTimeout(function(){ 
// add your code here
}, 3000);

For example, I’m using it to remove the z-index after the same. However, once inside the function, you can’t use “this” so what I do is use with an arrow function

let mouseLeave = (elem) => {
    setTimeout(function () {
        elem.parentNode.style.zIndex = '1';
    }, 500);
}

The above is the arrow function. Below is how you’d use it inside another function

function mouseLeaveFunction() {
    this.parentNode.classList.remove('active');
    mouseLeave(this);
}

Have any questions or comments? Write them below!


Leave a Reply

Your email address will not be published. Required fields are marked *