In the Paladin group, we're creating a game engine called Gladius, and
we're actively discussing how to implement very fundamental things
like schedulers.
Currently, we're able to circumvent the limit imposed on traditional
looping techniques (setTimeout, setInterval, requestAnimationFrame) by
using a trick with postMessage we discovered in the great ether of the
internet.
If a handler is setup to listen for "message" events on the window, it
will receive messages from a call to "postMessage( 'anythingYouWant',
'*' );". If that handler in turn calls the same (or similar)
postMessage, the handler will be called again, giving us a loop.
The problem is that this loop actually runs pretty quick -- a lot
quicker than "setTimeout( 0 )". Moreover, it's not quite the same as
simply doing "while(true) {}", because the browser tries to handle the
messaging done with postMessage. So, you do get some responsiveness
from the browser. However, whichever core on which this code is run is
quickly consumed and, frequently, my window manager starts to implode.
I suppose my question is what's a game developer to do? I'm certainly
not convinced that this technique makes any sense because it's
actually a hack and, for the sake of the web, it should probably be
stopped by browser vendors (or else say goodbye to your laptop
battery). Using setTimeout is always an option, but from the
standpoint of a traditional game developer, it's less optimal than a
main game loop that runs as fast as possible. In JS, we just don't
have a way to yield to the browser afaik (like yielding to the OS in
the traditional game dev sense).
You can check out an example here:
http://jsfiddle.net/4GxNh/14/
Click the "Slow" button to run a traditional loop. The timer reads
about 3 or 4 on my machine running Chromium. Click the "Fast" button
to run the postMessage hack and the timer should read 0 or 1, and your
CPU usage will go through the roof (p.s. SAVE YOUR WORK FIRST).