This is a simple description on how to use the timer and interval functionalities build in to JavaScript.
Timer function
The purpose of a timer function is to delay execution of code for x number of milliseconds.
Here’s an example:
var timer = setTimeout(function () {
– Your code goes here!
}, 250);
If you want to abort this timer, you can call the clearTimeout() and pass in the timer object as the argument.
clearTimeout(timer);
Interval function
The purpose of the interval function is to execute code repeatedly with a time interval.
This example will execute code every 250 milliseconds.
var interval = setInterval(function () {
– Your code goes here!
}, 250);
And here is how you cancel the interval.
clearInterval(interval);