wait(frames)
,它可以使函数在继续之前等待一些帧。
我有一个像这样的主循环:
setInterval(function() {
refreshMap();
}, 35 );
这样的功能:
function sayHello(){
console.log("hello 1");
wait(5);
console.log("hello 2");
}
wait(5)
函数会在执行console.log("hello 2");
之前使游戏刷新5次。是否可以不冻结整个游戏,如setTimeout()
?
答案 0 :(得分:0)
我看不到你的第一和第二个功能如何相互作用。但是这样的事情会让游戏等待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");
}