multithreading - Can my javascript function be interrupted by an animation frame -
i have learned requestanimationframe , how can used schedule animation code in sync browser's internal draw loop. still bit unclear how browser handles type of scheduling. interrupt scripts if necessary perform animations?
consider next bit of javascript code:
function dostuff() { var animated = false; var animation = function() { // perform animation animated = true; } requestanimationframe(animation); // perform more operations here // q: animated guaranteed false here?? }
i know javascript single-threaded gut feeling under no circumstances should animation()
have been called when reach lastly line of dostuff()
. here can give more informed answer.
you're correct, callback won't called until @ to the lowest degree phone call dostuff
has completed.
the nascent specification animation stuff refers html5 spec's concept of queuing task window's event loop. same concept used settimeout
, setinterval
, when time has expired, , means while callback queued @ moment in time, browser waits until main javascript ui thread next available before executing next task in queue. same thing happens events; event can happen @ time, including while javascript code running, phone call handler queue until main ui thread free handle it.
i know javascript single-threaded...
javascript, language, silent on concept of threading. in browsers, there's 1 main thread can interact dom, , 0 or more web worker threads can't (they can other things, , exchange messages main ui thread, arrive event callbacks, are...queued above :-) ).
javascript multithreading animation requestanimationframe
No comments:
Post a Comment