Timers Function in JavaScript
This article will discuss on the timer related function used in JavaScript Programming. These functions performs the job of executing piece of code/function after specific interval/time. JavaScript comes with in-build timers related function to achieve this.
Timers related functions in JavaScript are
1. setTimeout()
This allows you to execute a piece of code/function after certain duration. This function will call the code/function only once. This function returns an value.
Syntax:
var x=setTimeout(js statement/function , milliseconds);
The setTimeout() function return a value. The return value is stored in a variable called x. If you want to cancel this setTimeout(), you can refer to the variable name and call clearTimeout function.
2. clearTimeout()
This function cancels the Timeout that was set using setTimeout function.
Syntax:
var x=setTimeout(js statement/function , milliseconds); clearTimeout(x);
Here i am clearing the timeout call by calling variable x in the parameter.
3. setInterval()
This allows you to execute a piece of code/function after certain duration. This function will call the code/function infinite unless you clear the setInterval by calling clearInterval or close the page. This function returns an value.
Syntax:
var y=setInterval(js statement / function,milliseconds);
Here the setInterval() function return a value which gets stored in a variable called y. If you want to clear/cancel this setInterval(), you can refer to the variable name and call clearInterval function.
4. clearInterval()
This function cancels the Timeout that was set with the setInterval Function.
Syntax:
var y=setInterval(js statement / function,milliseconds); clearInterval(y);
Here i am clearing the Timeout by calling variable y in the parameter.
Similar Posts
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.


Comments
No comments yet.
Leave a comment