Timer and Interval Offset

John Resig continues to explore the timer objects in JavaScript language, this time he write:

«ome Archives Projects About Me

John Resig

Contact, Subscribe

Timer and Interval Offset

I've got another crazy-weird setTimeout/setInterval behavior that you may not know about. However, unlike my previous discovery, this one may actually be useful.

Observe the following, seemingly innocuous, code:

var count = 0;

var interval = setInterval(function(off){

document.body.innerHTML += " " + off;

if ( ++count == 10 )

clearInterval( interval );

}, 100);

In particular pay attention to the use of the first argument within the callback function. Running this code in any browser, but a Mozilla based one, will give you the expected "undefined undefined undefined ...".»

You can read the full version of article in John Resig blog.

Advertise on this site

Recent Comments