[whatwg] <video> ... <script> race condition

Glenn Maynard glenn at zewt.org
Sun May 15 10:11:09 PDT 2011

On Sat, May 14, 2011 at 11:49 AM, Eric Carlson <eric.carlson at apple.com>wrote:

>  It seems to me that the right way to "fix" the problem is let people know
> it is sloppy code, not to figure out a way to work around it.

The basic problem is that it isn't sloppy code: it's correct for almost all
events.  It's just wrong for events that are fired as part of loading, which
is what makes it so easy to get wrong.  It also means that these events
don't work well with deferred scripts.

For the simple cases I'd probably do:

video.addEventListener("canplay", func, false);
if(video.readyState >= HAVE_FUTURE_DATA)
    func.apply(video); // missed the first one

If a MediaController is being used it's more complicated; there seems to be
no way to query the readyState of a MediaController (almost, but not quite,
the "most recently reported readiness state"), or to get a list of slaved
media elements from a MediaController without searching for them by hand.

Image loading and Canvas is another aspect of this.  People often try to
render an image to a canvas from inline scripts without waiting for them to
load.  Since the image is probably cached while you're debugging, it's easy
for this to be masked.

Glenn Maynard

More information about the whatwg mailing list