Javascript:等待几帧后再继续功能
Javascript : Wait few frames before continue function
我正在开发一款游戏,遇到了一些麻烦。我想要一个函数wait(frames)
,它将使函数在继续之前等待一些帧。
我有一个这样的主循环:
setInterval(function() {
refreshMap();
}, 35 );
还有一个类似的函数:
function sayHello(){
console.log("hello 1");
wait(5);
console.log("hello 2");
}
在执行console.log("hello 2");
之前,wait(5)
功能将使游戏刷新5次。有没有可能不像setTimeout()
那样冻结整个游戏?
我看不出第一个和第二个函数是如何交互的。但这样的事情会让游戏等待10秒而不会冻结游戏:
setInterval(refreshMap,35);
sayHello();
function sayHello(timeToContinue)
{
if (!timeToContinue) //the first time it's called
{
console.log("hello 1");
var nextTime = new Date();
nextTime.setSeconds(nextTime.getSeconds() + 10);
window.setTimeout(function(){sayHello(nextTime);}), 1); //call this function again
return;
}
if (new Date() < timeToContinue) //when next the function gets here check 10 seconds have passed
{
window.setTimeout(function(){sayHello(timeToContinue);}), 1); //else come back later
return;
}
console.log("hello 2");
}
相关文章:
- 等待函数执行后再继续
- 如何让foreach循环等待$http.get完成后再继续
- 如何在Node.js中等待函数完成后再继续
- 等待AJAX响应后再继续
- 等待 AJAX,然后再继续执行单独的函数
- 使用生成器等待输入,然后再继续 forEach 循环的主体
- 如何使用 JQuery 添加延迟?(让它等待 5 秒再继续)
- 等待多个 ipc 调用完成,然后再继续电子/冗余
- 如何等待findOneAndUpdate完成,然后再继续async.series
- 等待 API 调用在 Javascript 中完成,然后再继续
- Angular2 可观察 - 等待多个函数调用,然后再继续
- HTML5 视频在设置当前帧后继续播放
- 等待方法的响应,然后再继续而不使用 SJAX
- 等待 .each 完成,然后再继续 (jQuery)
- 如何为性能执行多个异步函数(非嵌套),但等待它们完成再继续
- 在JavaScript或Jquery中,让循环等待按钮按下后再继续
- Javascript/jQuery使函数等待回调后再继续
- 等待图像加载后再继续
- 如果我屈服于承诺,它会等待承诺得到解决后再继续吗
- Javascript:等待几帧后再继续功能