Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 992847 of timers

  • Revision slug: Mozilla/Add-ons/SDK/High-Level_APIs/timers
  • Revision title: timers
  • Revision id: 992847
  • Created:
  • Creator: wbamberg
  • Is current revision? Yes
  • Comment
Tags: 

Revision Content

{{AddonSidebar}}

Stable

Set one-off and periodic timers.

Globals

Constructors

Functions

setTimeout(callback, ms)

Schedules callback to be called in ms milliseconds. Any additional arguments are passed straight through to the callback.

Parameters

callback : function
Function to be called.

ms : integer
Interval in milliseconds after which the function will be called.

Returns

integer : An ID that can later be used to undo this scheduling, if callback hasn't yet been called.

Example
var { setTimeout } = require("sdk/timers");

setTimeout(function() {
  // do something in 0 ms
}, 0)

clearTimeout(ID)

Given an ID returned from setTimeout(), prevents the callback with the ID from being called (if it hasn't yet been called).

Parameters

ID : integer
An ID returned from setTimeout().

Example
var { setTimeout, clearTimeout } = require("sdk/timers");

var id = setTimeout(function() {
  // do something in 1 sec
}, 1000);

// to stop/cancel this timeout
clearTimeout(id);

setInterval(callback, ms)

Schedules callback to be called repeatedly every ms milliseconds. Any additional arguments are passed straight through to the callback.

Parameters

callback : function
Function to be called.

ms : integer
Interval in milliseconds at which the function will be called.

Returns

integer : An ID that can later be used to unschedule the callback.

Example
var { setInterval } = require("sdk/timers");

setInterval(function() {
  // do something every 1 sec
}, 1000)

clearInterval(ID)

Given an ID returned from setInterval(), prevents the callback with the ID from being called again.

Parameters

ID : integer
An ID returned from setInterval().

Example
var { setInterval, clearInterval } = require("sdk/timers");

var id = setInterval(function() {
  // do something every 1 sec

  // to stop/cancel this interval
  clearInterval(id);
}, 1000);

Revision Source

{{AddonSidebar}}

<div class="note">
<p>Stable</p>
</div>

<p><span class="seoSummary">Set one-off and periodic timers.</span></p>

<h2 id="Globals">Globals</h2>

<h3 id="Constructors">Constructors</h3>

<h3 id="Functions">Functions</h3>

<h4 class="addon-sdk-api-name" id="setTimeout(callback_ms)"><code>setTimeout(callback, ms)</code></h4>

<p>Schedules <code>callback</code> to be called in <code>ms</code> milliseconds. Any additional arguments are passed straight through to the callback.</p>

<h5 id="Parameters">Parameters</h5>

<p><strong>callback : function</strong><br />
 Function to be called.</p>

<p><strong>ms : integer</strong><br />
 Interval in milliseconds after which the function will be called.</p>

<h5 id="Returns">Returns</h5>

<p><strong>integer</strong> : An ID that can later be used to undo this scheduling, if <code>callback</code> hasn't yet been called.</p>

<h5 id="Example">Example</h5>

<pre class="brush: js">
var { setTimeout } = require("sdk/timers");

setTimeout(function() {
  // do something in 0 ms
}, 0)
</pre>

<h4 class="addon-sdk-api-name" id="clearTimeout(ID)"><code>clearTimeout(ID)</code></h4>

<p>Given an ID returned from <code>setTimeout()</code>, prevents the callback with the ID from being called (if it hasn't yet been called).</p>

<h5 id="Parameters_2">Parameters</h5>

<p><strong>ID : integer</strong><br />
 An ID returned from <code>setTimeout()</code>.</p>

<h5 id="Example_2">Example</h5>

<pre class="brush: js">
var { setTimeout, clearTimeout } = require("sdk/timers");

var id = setTimeout(function() {
  // do something in 1 sec
}, 1000);

// to stop/cancel this timeout
clearTimeout(id);
</pre>

<h4 class="addon-sdk-api-name" id="setInterval(callback_ms)"><code>setInterval(callback, ms)</code></h4>

<p>Schedules <code>callback</code> to be called repeatedly every <code>ms</code> milliseconds. Any additional arguments are passed straight through to the callback.</p>

<h5 id="Parameters_3">Parameters</h5>

<p><strong>callback : function</strong><br />
 Function to be called.</p>

<p><strong>ms : integer</strong><br />
 Interval in milliseconds at which the function will be called.</p>

<h5 id="Returns_2">Returns</h5>

<p><strong>integer</strong> : An ID that can later be used to unschedule the callback.</p>

<h5 id="Example_3">Example</h5>

<pre class="brush: js">
var { setInterval } = require("sdk/timers");

setInterval(function() {
  // do something every 1 sec
}, 1000)
</pre>

<h4 class="addon-sdk-api-name" id="clearInterval(ID)"><code>clearInterval(ID)</code></h4>

<p>Given an ID returned from <code>setInterval()</code>, prevents the callback with the ID from being called again.</p>

<h5 id="Parameters_4">Parameters</h5>

<p><strong>ID : integer</strong><br />
 An ID returned from <code>setInterval()</code>.</p>

<h5 id="Example_4">Example</h5>

<pre class="brush: js">
var { setInterval, clearInterval } = require("sdk/timers");

var id = setInterval(function() {
  // do something every 1 sec

  // to stop/cancel this interval
  clearInterval(id);
}, 1000);
</pre>
Revert to this revision