setTimeout and setInterval - timed events
Need to animate something? This are the two calls in your browser you really need to know about.
How do they work?
One passes a Javascript function as the first argument and the time in milliseconds before the that function will be called. For example:
setTimeout(function(){
console.log("Breakdance now!");
}, 3000);
This will call the function defined 3 seconds after the code is run.
What is the difference between setInterval and setTimeout?
setInterval will repeat indefinitely until you cancel it.
How can I cancel a timer?
This will work:
var TIMid = setTimeout(function() { console.log("Hello");}, 3000);
clearTimeout(TIMid);
Notice you have to keep track of the ID in a Javascript variable and then use it again?
The same method can be used with setInterval.