/
setTimeout and setInterval - timed events

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.

Related content

Progress function for animation effects from first principles
Progress function for animation effects from first principles
More like this
Source code to the progress function
Source code to the progress function
More like this
Simple banner code
Simple banner code
More like this
Javascript Closure
Javascript Closure
More like this
Move a div to an absolute position
Move a div to an absolute position
Read with this
How can I change variables outside of my function with my timer?
How can I change variables outside of my function with my timer?
More like this