Add-ons using the techniques described in this document are considered a legacy technology in Firefox. Don't use these techniques to develop new add-ons. Use WebExtensions instead. If you maintain an add-on which uses the techniques described here, consider migrating it to use WebExtensions.

Starting from Firefox 53, no new legacy add-ons will be accepted on addons.mozilla.org (AMO) for desktop Firefox and Firefox for Android.

Starting from Firefox 57, only extensions developed using WebExtensions APIs will be supported on Desktop Firefox and Firefox for Android.

Even before Firefox 57, changes coming up in the Firefox platform will break many legacy extensions. These changes include multiprocess Firefox (e10s), sandboxing, and multiple content processes. Legacy extensions that are affected by these changes should migrate to use WebExtensions APIs if they can. See the "Compatibility Milestones" document for more information.

A wiki page containing resources, migration paths, office hours, and more, is available to help developers transition to the new technologies.

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);

Document Tags and Contributors

Tags: 
 Contributors to this page: wbamberg, evold
 Last updated by: wbamberg,