What Is setTimeout() And How To Use It? - Save My Knowledge
Wednesday, May 2, 2018

What Is setTimeout() And How To Use It?

Hi, now I want to share javascript function that might help us to develop something, this function is to call functions or run code after some time (to delay the execution code after certain time). The function setTimeout() is function from javascript itself that we can call it even we didn't initialize it. Not only it can delay call the function, but this function can make browser to skip this line of code for a while and call the code inside it after certain time.

This function have 2 parameter:
  1. Function to be executed
  2. Timeout time per millisecond

Example #1:
setTimeout(function() {
    alert("timeout testing");
}, 1000);
 Explanation: code alert "timeout testing" will be run after 1 second setTimeout executed.

Example #2:
function runAlert() {
    alert("inside function");
}
setTimeout(runAlert(), 2000);
Explanation: function runAlert() will be run after 2 second setTimeout code executed.

That's all for setTimeout javascript function and I hope this is helpful 😇