W3C home > Mailing lists > Public > public-script-coord@w3.org > July to September 2011

Re: Strict mode callbacks and setTimeout

From: Boris Zbarsky <bzbarsky@MIT.EDU>
Date: Fri, 08 Jul 2011 23:39:50 -0400
Message-ID: <4E17CD86.9010005@mit.edu>
To: Ian Hickson <ian@hixie.ch>
CC: "Mark S. Miller" <erights@google.com>, Allen Wirfs-Brock <allen@wirfs-brock.com>, Luke Hoban <lukeh@microsoft.com>, "es5-discuss@mozilla.org" <es5-discuss@mozilla.org>, public-script-coord@w3.org
On 7/8/11 6:01 PM, Ian Hickson wrote:
>> That's unclear.  While there is only one task queue, the list of active
>> timeouts is per-window.
>
> Indeed. setTimeout also depends on the "fully active" state of the
> Document of its Window.

Ah, yes.  That part is important too.

> It means literally what it says, i.e. globally across the whole universe,
> within, of course, the limitations of the hardware.

That doesn't make much sense to me.  For example, it means that one tab 
that has posted a timeout and then went into an infinite loop should 
block timeouts in unrelated sites.

> This might be too strong a requirement, though. I'm happy to scope it somewhat if that would
> make more sense. Scoping it to the Window objects using the same event
> loop probably makes the most sense; would that be better?

I'm not sure it would be enough better.  As the spec is currently 
phrased, that would mean that a setTimeout(0) call on a background-tab 
window would delay a later setTimeout(0) call on a foreground window 
that is on the same event loop.  That somewhat defeats the point of 
throttling background windows.

What Gecko does right now is that the "if two timeouts are set for the 
same delay then the one that was set earlier should fire earlier" thing 
applies per-window.  That is, the list of timeouts for a window is 
stored in firing order, but nothing per se guarantees the relative 
ordering of timeouts in two separate windows.

In particular, in Gecko's current implementation if you have two windows 
A and B and then you perform the following actions:

  A.setTimeout(f1, 0);
  B.setTimeout(f2, 0);
  A.setTimeout(f3, 100);
  B.setTimeout(f4, 100);
  var start = new Date();
  while ((new Date() - start) < 1000) ;

and return to the event loop, then the firing order of the callbacks 
will definitely be either "f1, f3, f2, f4" or "f2, f4, f1, f3" (because 
we process all the "old enough" timeouts for a window before moving on 
to another window).  I suspect in this particular case we actually 
guarantee that it will be "f1, f3, f2, f4".  Attached is a testcase for 
this behavior using subframes.  In the case of Gecko that doesn't affect 
the results; any other two windows would give the same behavior.  This 
last claim may or may not be true for other browsers.

I just tested locally, and on my testcase Opera seems to produce "f1, 
f3, f2, f4" just like Gecko.  Safari, Chrome, and IE9 (in all modes) all 
produce "f1, f2, f3, f4".  Again, the behavior here may depend on 
whether things are in different tabs or not.  Worth checking with other 
implementors.

-Boris



Received on Saturday, 9 July 2011 03:40:21 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 8 May 2013 19:30:04 UTC