Shouldn't timers be specified?

Brandon Benvie brandon at brandonbenvie.com
Sat Jan 21 21:36:45 PST 2012


For reference, here's the start of an es-discuss thread on the topic from
March 2011:
https://mail.mozilla.org/pipermail/es-discuss/2011-March/013129.html

Here's a reference on implementing timer functions in Rhino:

http://stackoverflow.com/questions/2261705/how-to-run-a-javascript-function-asynchronously-without-using-settimeout


You can use java.util.Timer<http://download.oracle.com/javase/1.5.0/docs/api/java/util/Timer.html>
 and java.util.TimerTask<http://download.oracle.com/javase/1.5.0/docs/api/java/util/TimerTask.html>
to
roll your own set/clear Timeout and set/clear Interval functions:

var setTimeout,
    clearTimeout,
    setInterval,
    clearInterval;

(function () {
    var timer = new java.util.Timer();
    var counter = 1;
    var ids = {};

    setTimeout = function (fn,delay) {
        var id = counter++;
        ids[id] = new JavaAdapter(java.util.TimerTask,{run: fn});
        timer.schedule(ids[id],delay);
        return id;
    }

    clearTimeout = function (id) {
        ids[id].cancel();
        timer.purge();
        delete ids[id];
    }

    setInterval = function (fn,delay) {
        var id = counter++;
        ids[id] = new JavaAdapter(java.util.TimerTask,{run: fn});
        timer.schedule(ids[id],delay,delay);
        return id;
    }

    clearInterval = clearTimeout;

})()

Looking for the Spidermonkey equivalent currently. Regardless, it
seems like something that needs to be implemented at the language
level, and has wide support for it. There's a lot on the table for ES6
and it's pretty late in the game (well after technical feature freeze)
so I definitely see why it wouldn't make the cut. But if not for ES6,
this needs to be in ES next text, and as something already implemented
in nearly all JS envs, simply informally agreeing on the fundamentals
would be enough to get it most of the way there implementation-wise in
most implementations.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120122/9e7a674f/attachment-0001.html>


More information about the es-discuss mailing list