Bringing setTimeout to ECMAScript

Breton Slivka zen at
Sat Mar 19 06:42:59 PDT 2011

> I can't think of a single way to simulate setTimeout in ES5. Correct me if
> I'm wrong, but I don't think ES5 exposes a single way of defining a
> mechanism like:
> --
> var x = 4;
> function f(){ x = 5; print(x); }
> timer(f, 1);
> print(f);
> --
> Such that it would output 4 before 5. That's what I meant with "didn't have
> any asynchronism", fwiw.

while (!programHasQuit()) {

 timers= (function () {
      var timers = [];
      var id=0;
      timer=function (f,t) {
           timers.push({func:f, interval:t, id:id++});
           return id;
     return timers;



and there you go, in pure JS. If this is a gui program, you may expose
a queue of GUI events to this "master script", but I believe that the
event loop is best left to the embedding. If setTimeout etc are
implemented in the core JS engine, the JS engine can simply expose the
pool of timers as some data structure to the embedded C/C++ program to
do with what you wish- The standard would presumably specify some
guidelines for this.

More information about the es-discuss mailing list