Revision 362245 of Timer.jsm

  • Revision slug: Mozilla/JavaScript_code_modules/Timer.jsm
  • Revision title: Timer.jsm
  • Revision id: 362245
  • Created:
  • Creator: MattBrubeck
  • Is current revision? No
  • Comment use a better example, since alert() depends on the DOM

Revision Content

{{ gecko_minversion_header("22") }}

The Timer.jsm JavaScript code module contains pure-JavaScript implementations of setTimeout and clearTimeout that are compatible with the DOM window functions, but that can be used by code that does not have access to a DOM window (for example, JavaScript code modules or content frame scripts).

To use Timer.jsm, first import it:

Components.utils.import("resource://gre/modules/Timer.jsm");

Then call setTimeout and clearTimeout just as you would in a DOM window, for example:

var timeoutID = setTimeout(function() { console.log("Hello!"); }, 500);

clearTimeout(timeoutID);

Revision Source

<p>{{ gecko_minversion_header("22") }}</p>
<p>The <code>Timer.jsm</code> JavaScript code module contains pure-JavaScript implementations of <a href="/en-US/docs/DOM/window.setTimeout" title="/en-US/docs/DOM/window.setTimeout"><code>setTimeout </code></a>and <a href="/en-US/docs/DOM/window.clearTimeout" title="/en-US/docs/DOM/window.clearTimeout"><code>clearTimeout</code></a> that are compatible with the DOM window functions, but that can be used by code that does not have access to a DOM window (for example, <a href="/en-US/docs/Mozilla/JavaScript_code_modules" title="/en-US/docs/Mozilla/JavaScript_code_modules">JavaScript code modules </a>or <a href="/en-US/docs/The_message_manager" title="/en-US/docs/The_message_manager">content frame scripts</a>).</p>
<p>To use <code>Timer.jsm</code>, first import it:</p>
<pre class="brush: js">
Components.utils.import("resource://gre/modules/Timer.jsm");
</pre>
<p>Then call <code>setTimeout </code>and <code>clearTimeout </code>just as you would in a DOM window, for example:</p>
<pre class="brush: js">
var timeoutID = setTimeout(function() { console.log("Hello!"); }, 500);

clearTimeout(timeoutID);
</pre>
Revert to this revision