0

requestAnimationFrame 采用一个回调函数,该函数在显示帧之前运行。假设这个回调函数有副作用:

function callback {
  ++GLOBAL_VAR_A;
  ++GLOBAL_VAR_B;
}

如果我对该回调的请求调用cancelAnimationFrame,回调会是原子的吗?换句话说,回调是保证完全执行还是根本不执行?或者取消休假可以说GLOBAL_VAR_A增加,但不是GLOBAL_VAR_B

此外,如果这个回调是原子的,如果我成功取消框架,回调会执行还是不执行?

4

1 回答 1

1

Javascript is mono-threaded, so this problem cannot show : either the callback was called or not when the cancelAF is called. If it was called, nothing is done. If it was not called, it won't get called later. Your question suggest that cancelAF would also cancel an ongoing callback, but the specification dooesn't say a word about that : MDN description is :

window.cancelAnimationFrame

Cancels an animation frame request previously scheduled through a call to window.requestAnimationFrame().

( https://developer.mozilla.org/en-US/docs/Web/API/window.cancelAnimationFrame ).

It is a 'request' which gets cancelled, not the callback.

Maybe to be a little clearer : if you call cancelAF after ++GLOBAL_VAR_A and before ++GLOBAL_VAR_B;, then it means the callback was allready called and the cancelAF does just nothing.

cancelAF has no return value, so you cannot tell you "cancel(led) the frame successfully"

于 2013-06-13T19:24:18.813 回答