Module PeriodicalExecuter
PeriodicalExecuter <ul><li>This is a simple facility for periodical execution of a function.  </li></ul><ul><li>This essen...
Creating a PeriodicalExecuter  <ul><li>The constructor takes two arguments the callback function, and the interval (in  se...
Stop a PeriodicalExecuter <ul><li>stop() </li></ul><ul><li>Stops the periodical executer (there will be no further trigger...
Upcoming SlideShare
Loading in …5
×

Periodicalexecuter

836 views
788 views

Published on

Prototype Javascript

Published in: Spiritual, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
836
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Periodicalexecuter

  1. 1. Module PeriodicalExecuter
  2. 2. PeriodicalExecuter <ul><li>This is a simple facility for periodical execution of a function. </li></ul><ul><li>This essentially encapsulates the native clearInterval/setInterval mechanism found in native Window objects </li></ul><ul><li>The only notable advantage provided by PeriodicalExecuter is that it shields you against multiple parallel executions of the callback function </li></ul><ul><li>This is especially useful if you use one to interact with the user at given intervals (e.g. use a prompt or confirm call): </li></ul><ul><li>This will avoid multiple message boxes all waiting to be actioned. </li></ul>
  3. 3. Creating a PeriodicalExecuter <ul><li>The constructor takes two arguments the callback function, and the interval (in seconds ) between executions. </li></ul><ul><li>Once launched, a PeriodicalExecuter triggers indefinitely, until the page unloads or the executer is manually stopped </li></ul><ul><li>Example; </li></ul><ul><li>// Campfire style :-) </li></ul><ul><li>   new PeriodicalExecuter(pollChatRoom,  3 ); </li></ul><ul><li>   new PeriodicalExecuter( function (pe) { </li></ul><ul><li>     if  (!confirm( 'Want me to annoy you again later?' )) </li></ul><ul><li>       pe.stop();  },  5 ); </li></ul><ul><li>   // Note that there won't be a stack of such messages if the user takes  too long answering to the question... </li></ul><ul><li>  </li></ul><ul><li>  </li></ul><ul><li>  </li></ul>
  4. 4. Stop a PeriodicalExecuter <ul><li>stop() </li></ul><ul><li>Stops the periodical executer (there will be no further triggers). </li></ul><ul><li>Once a PeriodicalExecuter is created, it constitues an infinite loop, triggering at the given interval until the page unloads. </li></ul><ul><li>Example; </li></ul><ul><li>var gCallCount =  0 ; </li></ul><ul><li>new PeriodicalExecuter( function (pe) { </li></ul><ul><li>   if  (++gCallCount >  3 ) </li></ul><ul><li>     pe.stop(); </li></ul><ul><li>   else </li></ul><ul><li>     alert(gCallCount);} ,  1 ); </li></ul><ul><li>// Will only alert 1, 2 and 3, then the PE stops. </li></ul><ul><li>  </li></ul>

×