Fixing Recursive onResize Calls on the iPad (iOS 4.2 and 4.3)

We're writing Pageforest applications for the iPad (e.g., Quoridor).  One really confusing issue popped up that seems to be a bug only when running in the full-screen mode of the iPad.

When we turn the display from portrait to landscape we would get a window.resize event.  But then, as a side-effect of measuring a DOM element (like reading elem.offsetHeight), a RECURSIVE CALL to our window.resize event handler is called.  I couldn't believe it - but it's true.  This is the one case I've seen where the browser makes a re-entrant call to an event handling function.

To fix it, we created a simple function to serialize all calls to our real event handler:

    $(window).resize( function () { setTimeout(onResize, 0); } );

Now all calls to our onResize function will be serialized, instead of occuring re-entrantly while we are in the middle of handling the first resize event.

We're not sure why our app is getting two calls to resize during a portrait to landscape flip - it looks like it might be giving us a smaller intermediate size, for the purposes of displaying a rotation animation of the page to the new orientation.