Bringing setTimeout to ECMAScript

Peter van der Zee ecma at qfox.nl
Sat Mar 19 07:14:02 PDT 2011


On 19 Mar 2011 14:43, "Breton Slivka" <zen at zenpsycho.com> wrote:
>
> > 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;
>  })
>
>  runScript("myscript.js");
>  handleEvents(timers,otherevents);
>
> }
>
>
> 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.

That only works if the host environment isn't waiting for you to finish /
wait / sleep / yield / etc. But yeah, ok :)

- peter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110319/14d9a4ad/attachment-0001.html>


More information about the es-discuss mailing list