JavaScript SetTimeout

JavaScript SetTimeout Function
setTimeout is very useful function in JavaScript. setTimeout is native JavaScript function means its exist in core JavaScript you don't need to add any library to use this javascript function.You may also say that its a JavaScript delay method or one of the JavaScript timer function.
setTimeout function calls a function or executes a code or expression after a specified delay & this delay is in miliseconds. Its another good name is JavaScript wait method. setInterval is another function in JavaScript
setTimeout function is executed once. If you need to repeat execution you have to use the setInterval() function. we will discuss setInterval function in later post.
This function takes two arguments:
 1: The function or code which we have to call.
 2: The number of milliseconds [1000 millisecond=1 second] to wait before executing the code or function.

The most common use of setTimeout function is suppose visitor visit your blog or website and after some second you have to show popup then you do this with setTimeout timer method.
The simple example of setTimeout is given below.

setTimeout Example

Output of setTimeout Example

Other Example Of setTimeOut

function showPopup()
 alert("your javascript setTimeout is here");
 setTimeout(showPopup, 3000);

 you can also write above code in another way
 var showPopup=function ()
 alert("your javascript setTimeout is here");
 setTimeout(showPopup, 3000);

 Another way is
 setTimeout(function ()
 alert("your javascript setTimeout is here");
 }, 3000);

There is also possibility to pass the string in your function like below
setTimeout("alert("your javascript setTimeout is here"); ", 2000);


Post a Comment