Re: Execute Last

"Ian Petersen" <[email protected]> Tue, 21 Mar 2006 09:48:21 -0500
Newsgroups gmane.comp.web.dom.wdf
Message-ID <[email protected]>
Hi Moran,

> Does anyone know of a good way to delay execution of a piece of code without
> relying on time (as in setInterval and setTimeout)?

Can I ask why you're concerned about relying on time?

In my experience, all browsers run Javascript in a single thread,
which means you can never have parallel execution, even if you use
setTimeout.  So changing your code like this would do the trick:

var lastExecutors = []
var timeoutID = null

function executeLast(o, f) {
    lastExecutors.push([o, f])

    if(!timeoutID) {
        timeoutID = setTimeout(cycleLastExecutors, 1);
    }
}

function cycleLastExecutors() {
    timeoutID = null;

    var task;

    while(lastExecutors.length > 0) {
        task = lastExecutors.shift();

        task[1].apply(task[0]);
    }
}


With the above code, calling executeLast(foo, foo.last) will schedule
a call to foo.last() to be run 1 millisecond after the current thread
of execution terminates.  Is one millisecond too long for your
application?

Ian

--
Tired of pop-ups, security holes, and spyware?
Try Firefox: http://www.getfirefox.com


Unsubscribe
[email protected]

List info
http://www.quirksmode.org/dom/list.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/wdf-dom/

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/