创建两个函数之间的执行时间(纯javascript)
我希望在爆炸函数和重置函数之间有一个设定的时间量,如果我没有设定的时间量,代码会在执行函数之后快速执行重置函数。这使爆炸基本上看不见。我试着在重置功能上设置一个超时,但这并没有改变任何事情 代码如下:创建两个函数之间的执行时间(纯javascript),javascript,timing,Javascript,Timing,我希望在爆炸函数和重置函数之间有一个设定的时间量,如果我没有设定的时间量,代码会在执行函数之后快速执行重置函数。这使爆炸基本上看不见。我试着在重置功能上设置一个超时,但这并没有改变任何事情 代码如下: function loop(){ //game code up here if(out){ //run explode for certain amount of time then call reset function explode();
function loop(){
//game code up here
if(out){
//run explode for certain amount of time then call reset function
explode();
//setTimeout(reset, time)
reset();
}
}
setInterval(loop, 10)
在JavaScript 1.7中,使用yield with可以执行以下操作:
function loop(){
//game code up here
if(out){
//run explode for certain amount of time then call reset function
explode();
//setTimeout(reset, time)
yield to.sleep(.500)
reset();
}
}
如果您只想在纯javascript中使用它,您可以尝试以下操作:
function loop() {
//game code up here
if (out) {
explode();
}
}
function explode() {
//explode code goes here
setTimeout(reset, 3000);
}
loop();
正如上面回答中提到的@Channa一样,setTimeout将在1000毫秒后执行一次重置功能。最好是这样包装,否则重置功能会立即运行
setTimeout(function() { reset(); }, 1000);
此外,如果要重复调用函数,可以使用此选项
setInterval(reset,1000);
setInterval将在每1000毫秒后调用您的重置函数。如果async.js是一个库,我该如何获得它?我在回答中添加了简单的javascript方法更正:我想在设定的时间内重复调用explode函数几次,因为explode不是渲染。这是逻辑思维:)花时间思考。