所以我是新来的,道歉,如果这是一个基本的问题,但我看不到它在其他地方回答。等待requestAnimationFrame完成(通过回调)
我想写一个显示动画(使用pixijs)的webapp,然后显示一个请求响应的div。我的问题是,因为动画是使用requestAnimationFrame处理的,所以动画是异步发生的,所以响应和动画阶段同时发生(div立即出现并遮挡动画)。
现在我看了看周围,似乎共识似乎是使用回调函数,只有在执行完所有异步工作后才会触发回调函数,从而允许序列进度。
然而,requestAnimationFrame的形式为
requestAnimationFrame(update_screen_objects),
,但休息时,我尝试做:
requestAnimationFrame(update_screen_objects(response_phase_callback))
很明显,requestAnimationFrame不喜欢被传递一个本身具有回调函数。所以我的问题是:我应该怎样做动画循环以确保后续函数在动画完成后执行?
谢谢!
EDIT
这是在上述形式不工作的代码的一个例子。
function animate(callback) {
var finished = false;
if ((new Date().getTime()) < adaptTime){
runFlicker(false);
} else if ((new Date().getTime()) < judgeTime){
hideAdaptors();
} else if ((new Date().getTime()) > judgeTime){
stage.visible = false;
finished = true;
}
renderer.render(stage);
if (!finished){
requestAnimationFrame(animate(callback)); //Ensures it will keep looping
//requestAnimationFrame(animate); //This does work, but my issue still persists
} else {
callback(); //By the time callback() is required, you can't access it as requestAnimationFrame doesn't accept animate() with a function passed to it.
}
}
'requestAnimationFrame'不会将参数传递给函数 – Markasoftware 2014-10-27 00:27:18
你的第二次尝试*调用函数,而不是*传递*。相反,传递一个调用'update_screen_objects'函数并传递回调的函数表达式。 – Bergi 2014-10-27 00:34:29
我们可以有你的代码的例子吗? – YoannM 2014-10-27 01:12:09