Skip to content Skip to sidebar Skip to footer

Css Animations Stall When Running Javascript Function

Let's say I have this javascript function: function pauseComp(ms) { var date = new Date(); var curDate = null; do { curDate = new Date(); } while(curDate-dat

Solution 1:

A browser page is single threaded. Updating the UI happens on the same thread as your javascript program. Which also means that any animation will not draw new frames while Javascript code is being executed. Typically, this is no big deal because most JS code is executed very quickly, faster than a single animation frame.

So the best advice is simply this: Don't do that. Don't lock up the JS engine for that long. Figure out a cleaner way to do it.


However, if you must, there is a way. You can get an additional thread via HTML5's Web Workers API. This isn't supported in older browsers, but it will allow you to run some long running CPU sucking code away from the main webpage and in it's own thread, and then have it post back some result to your page when it's done.

Post a Comment for "Css Animations Stall When Running Javascript Function"