开发者

Is there any "idiom" or pattern in JS that'll let me pause execution while an animation runs?

开发者 https://www.devze.com 2023-02-10 10:52 出处:网络
I basically have this code: for (var i = 0; i < num; i++) { ShowCard(i); } Right now, ShowCard is just adding a DOM element, but I want it to have an animation that\'ll show this card flying fro

I basically have this code:

for (var i = 0; i < num; i++) {
  ShowCard(i);
}

Right now, ShowCard is just adding a DOM element, but I want it to have an animation that'll show this card flying from somewhere else (the shoe) into its final destination, and I want the second card to wait until the first one is done flying before it starts.

Is there any way to achieve this, without rewriting my whole code in "continuation passing" style?

I'm assuming the answer is no and i'll have t开发者_如何学运维o bit the bullet and do it, but thought i'd ask.

Thanks!

Daniel


The answer is no.

The only blocking APIs in JavaScript are the built in ones - alert, confirm and prompt.


You can try jQuery's new deferred API.


You could put in a global locking variable inMotion that indicates the second card can't move. Then in the code that is calling for the second card to move, you can check this boolean and if it's not time to move, use setTimeout or setInterval to continuously call your method until it is time to move it.


Evil hack: Instead of making your animations work using setTimeout and friends (functions that give away the control flow), make them work the dumb way using while loops, spin lock style. In pseudo code:

next_time = curr_time + delta;
while(get_time() < next_time){} //Do nothing
0

精彩评论

暂无评论...
验证码 换一张
取 消