This is a snapshot of an early working draft and has therefore been superseded by the HTML standard.

This document will not be further updated.

HTML 5

Call For Comments — 27 October 2007

11.4. Timers

This section is expected to be moved to the Window Object specification in due course.

interface WindowTimers {
  // timers
  long setTimeout(in TimeoutHandler handler, in long timeout);
  long setTimeout(in TimeoutHandler handler, in long timeout, arguments...);
  long setTimeout(in DOMString code, in long timeout);
  long setTimeout(in DOMString code, in long timeout, in DOMString language);
  void clearTimeout(in long handle);
  long setInterval(in TimeoutHandler handler, in long timeout);
  long setInterval(in TimeoutHandler handler, in long timeout, arguments...);
  long setInterval(in DOMString code, in long timeout);
  long setInterval(in DOMString code, in long timeout, in DOMString language);
  void clearInterval(in long handle);
};

interface TimeoutHandler {
  void handleEvent(arguments...);
};

The WindowTimers interface must be obtainable from any Window object using binding-specific casting methods.

The setTimeout and setInterval methods allow authors to schedule timer-based events.

The setTimeout(handler, timeout[, arguments...]) method takes a reference to a TimeoutHandler object and a length of time in milliseconds. It must return a handle to the timeout created, and then asynchronously wait timeout milliseconds and then invoke handleEvent() on the handler object. If any arguments... were provided, they must be passed to the handler as arguments to the handleEvent() function.

In the ECMAScript DOM binding, the ECMAScript native Function type must implement the TimeoutHandler interface such that invoking the handleEvent() method of that interface on the object from another language binding invokes the function itself, with the arguments passed to handleEvent() as the arguments passed to the function. In the ECMAScript DOM binding itself, however, the handleEvent() method of the interface is not directly accessible on Function objects. Such functions must be called in the scope of the browsing context in which they were created.

Alternatively, setTimeout(code, timeout[, language]) may be used. This variant takes a string instead of a TimeoutHandler object. That string must be parsed using the specified language (defaulting to ECMAScript if the third argument is omitted) and executed in the scope of the browsing context associated with the Window object on which the setTimeout() method was invoked.

Need to define language values.

The setInterval(...) variants must work in the same way as the setTimeout variants except that the handler or code must be invoked again every timeout milliseconds, not just the once.

The clearTimeout() and clearInterval() methods take one integer (the value returned by setTimeout and setInterval respectively) and must cancel the specified timeout. When called with a value that does not correspond to an active timeout or interval, the methods must return without doing anything.

Timeouts must never fire while another script is executing. (Thus the HTML scripting model is strictly single-threaded and not reentrant.)