<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Prototype JavaScript framework - PeriodicalExecuter</title>
  <id>tag:www.prototypejs.org,2009:mephisto/api/periodicalExecuter</id>
  <generator uri="http://mephistoblog.com" version="0.7.3">Mephisto Noh-Varr</generator>
  <link href="http://www.prototypejs.org/feed/api/periodicalExecuter/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://www.prototypejs.org/api/periodicalExecuter" rel="alternate" type="text/html"/>
  <updated>2007-01-10T08:51:54Z</updated>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Christophe</name>
    </author>
    <id>tag:www.prototypejs.org,2007-01-10:13029</id>
    <published>2007-01-10T08:51:00Z</published>
    <updated>2007-01-10T08:51:54Z</updated>
    <category term="PeriodicalExecuter"/>
    <link href="http://www.prototypejs.org/api/periodicalExecuter/stop" rel="alternate" type="text/html"/>
    <title>stop</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;stop()&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Stops the periodical executer (there will be no further triggers).&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;stop()&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Stops the periodical executer (there will be no further triggers).&lt;/p&gt;
&lt;p&gt;Once a &lt;code&gt;PeriodicalExecuter&lt;/code&gt; is created, it constitues an infinite loop, triggering at the given interval until the page unloads.  This method lets you stop it any time you want.&lt;/p&gt;

&lt;p&gt;While there currently is a &lt;code&gt;registerCallback&lt;/code&gt; method that technically re-enables the executer, it is unclear whether it is considered internal (and therefore should not be used as a feature) or not.  In doubt, always instantiate a fresh &lt;code&gt;PeriodicalExecuter&lt;/code&gt; when you need to start one.&lt;/p&gt;

&lt;h3&gt;Examples&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
var gCallCount = 0;
new PeriodicalExecuter(function(pe) {
  if (++gCallCount &gt; 3)
    pe.stop();
  else
    alert(gCallCount);
}, 1);
// Will only alert 1, 2 and 3, then the PE stops.
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://www.prototypejs.org/">
    <author>
      <name>Christophe</name>
    </author>
    <id>tag:www.prototypejs.org,2007-01-10:13028</id>
    <published>2007-01-10T08:49:00Z</published>
    <updated>2007-03-13T06:56:13Z</updated>
    <category term="PeriodicalExecuter"/>
    <link href="http://www.prototypejs.org/api/periodicalExecuter" rel="alternate" type="text/html"/>
    <title>PeriodicalExecuter</title>
<content type="html">
            &lt;p&gt;This is a simple facility for periodical execution of a function.  This essentially encapsulates the native &lt;code&gt;clearInterval&lt;/code&gt;/&lt;code&gt;setInterval&lt;/code&gt; mechanism found in native &lt;code&gt;Window&lt;/code&gt; objects.&lt;/p&gt;

&lt;p&gt;The only notable advantage provided by &lt;code&gt;PeriodicalExecuter&lt;/code&gt; is that it shields you against multiple parallel executions of the callback function, should it take longer than the given interval to execute (it maintains an internal &#8220;running&#8221; flag, which is shielded against exceptions in the callback function).  This is especially useful if you use one to interact with the user at given intervals (e.g. use a &lt;code&gt;prompt&lt;/code&gt; or &lt;code&gt;confirm&lt;/code&gt; call): this will avoid multiple message boxes all waiting to be actioned.&lt;/p&gt;

&lt;p&gt;Of course, one might very well argue that using an actual object, not needing to maintain a global interval handle, etc. constitute notable advantages as well.&lt;/p&gt;

&lt;h3&gt;Creating a &lt;code&gt;PeriodicalExecuter&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;The constructor takes two arguments: the callback function, and the interval (in &lt;strong&gt;seconds&lt;/strong&gt;) between executions.  Once launched, a &lt;code&gt;PeriodicalExecuter&lt;/code&gt; triggers indefinitely, until the page unloads (which browsers usually take as an opportunity to clear all intervals and timers) or the executer is &lt;a href=&quot;/api/periodicalExecuter/stop&quot;&gt;manually stopped&lt;/a&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
// Campfire style :-)
new PeriodicalExecuter(pollChatRoom, 3);

new PeriodicalExecuter(function(pe) {
  if (!confirm('Want me to annoy you again later?'))
    pe.stop();
}, 5);
// Note that there won't be a stack of such messages if the user takes too long
// answering to the question...
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
</feed>
