This link has been bookmarked by 89 people . It was first bookmarked on 24 Feb 2008, by lilin_fr.
-
06 Apr 12
-
29 Jan 12
-
14 Nov 11
-
09 Nov 11
-
27 Sep 11
Jochen FrommJohn Resig explains how JavaScript timers work
-
22 Jun 11
-
26 Apr 11
-
01 Feb 11
-
04 Nov 10
-
07 Sep 10
-
24 Jul 10
-
29 Jun 10
-
28 Jun 10
Hyung-Joo LimJohn Resig - How JavaScript Timers Work http://ow.ly/17UsjT
– Javascript News (del_javascript) http://twitter.com/del_javascript/statuses/17222587485 -
10 May 10
-
03 May 10
-
30 Apr 10
-
26 Apr 10
-
15 Apr 10
-
Since all JavaScript in a browser executes on a single thread asynchronous events (such as mouse clicks and timers) are only run when there's been an opening in the execution.
-
Since JavaScript can only ever execute one piece of code at a time (due to its single-threaded nature) each of these blocks of code are "blocking" the progress of other asynchronous events. This means that when an asynchronous event occurs (like a mouse click, a timer firing, or an XMLHttpRequest completing) it gets queued up to be executed later
-
Intervals don't care about what is currently executing, they will queue indiscriminately, even if it means that the time between callbacks will be sacrificed.
-
- JavaScript engines only have a single thread, forcing asynchronous events to queue waiting for execution.
setTimeoutandsetIntervalare fundamentally different in how they execute asynchronous code.- If a timer is blocked from immediately executing it will be delayed until the next possible point of execution (which will be longer than the desired delay).
- Intervals may execute back-to-back with no delay if they take long enough to execute (longer than the specified delay).
There's a lot that we've learned here, let's recap:
-
-
01 Apr 10
Marg WilkinsonAt a fundamental level it's important to understand how JavaScript timers work. Often times they behave unintuitively because of the single thread which they are in. Let's start by examining the three functions to which we have access that can construct a
-
21 Dec 09
-
27 Nov 09
-
24 Aug 09
-
26 Jul 09
Kenneth PriisholmAt a fundamental level it's important to understand how JavaScript timers work. Often times they behave unintuitively because of the single thread which they are in. Let's start by examining the three functions to which we have access that can construct a
-
24 Jul 09
-
22 Jul 09
-
16 Jul 09
-
25 May 09
-
09 May 09
-
12 Sep 08
-
08 Sep 08
-
15 Apr 08
-
08 Apr 08
-
02 Mar 08
-
28 Feb 08
-
27 Feb 08
-
26 Feb 08
-
25 Feb 08
Jason WehmhoenerJohn Resig explains different timer implementations in context of JavaScript's single-thread.
-
24 Feb 08
-
-
var id = setTimeout(function(){ alert('hello'); }, 1000);
clearInterval(id);
-
-
-
setTimeout(function(){
/* Some long block of code... */
setTimeout(arguments.callee, 10);
}, 10);
setInterval(function(){
/* Some long block of code... */
}, 10);
-
-
Oncle TomExplications sur le fonctionnement des timers JavaScript en mode asynchrone.
javascript tips clevermarks timer javascript:settimeout javascript:setinverval javascript:clearinterval
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.