[Bug 15007] Add an API to queue a task

https://www.w3.org/Bugs/Public/show_bug.cgi?id=15007

Simon Pieters <simonp@opera.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WORKSFORME

--- Comment #16 from Simon Pieters <simonp@opera.com> 2011-12-05 10:17:40 UTC ---
(In reply to comment #14)
> However, I notice you can use MessageChannel for this purpose, without any of
> these problems.
> 
> var queueTask = function(task)
> {
>     var mc = new MessageChannel();
>     mc.port1.onmessage = task;
>     mc.port2.postMessage(null);
> }
> 
> queueTask(function() { console.log("test"); });

Nice find! Improved version with support for passing arguments and correct
'this':

var queueTask = function(task)
{
    var mc = new MessageChannel();
    var args = [].slice.call(arguments, 1);
    mc.port1.onmessage = function(){ task.apply(task, args); };
    mc.port2.postMessage(null);
}
queueTask(function(arg) { console.log(arg, this) }, "test");

Not as convenient as having it natively, but certainly works for me.

-- 
Configure bugmail: https://www.w3.org/Bugs/Public/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.

Received on Monday, 5 December 2011 10:34:38 UTC