Programming web browsers requires a different way of thinking. I always like to think in terms of linear progression, sequences that run from beginning to end. Web browsers want you to run a javascript function that has a time limit. If you exceed the time limit, the browser doesn't like it. While your function is running, the browser won't update the screen.
So you have to break up your long-running function into small enough bites that will run for a bit, then give control back to the browser. Of course, this plays havoc with any kind of structured programming, like being in a complex if-then block or somewhere inside of a for loop. One has to figure out a nice way to bring the function back to what it was doing. It makes my head hurt sometimes.
It'd be nice to just call a function like "coop_sleep(x);" which would cooperatively give control back to the browser for x milliseconds so it could update the screen and then allow you to continue execution.
Instead, we have to orchestrate workarounds for javascript's single threaded nature.
A couple of webpages about the issue:
http://javascript.info/tutorial/events-and-timing-depth see Script taking too long and heavy jobs
http://www.sitepoint.com/multi-threading-javascript/
No comments:
Post a Comment