This demo of a coroutine event loop uses features of ECMAScript 6 and thus only works in recent versions of Firefox or Chrome.
Try dragging the box around, and observe how the code behaves:
var loop = coroutine(function*() { var event; while (event = yield) { // not dragging if (event.type == 'mousedown') { while (event = yield) { // dragging if (event.type == 'mousemove') move(event); if (event.type == 'mouseup') break; } } // ignore mousemoves } }); $('#box').mousedown(loop); $(window).mousemove(loop).mouseup(loop);
You can read more about this.