Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在函数完成后重新启动它-Javascript_Javascript_Html_Canvas_Infinite Loop - Fatal编程技术网

如何在函数完成后重新启动它-Javascript

如何在函数完成后重新启动它-Javascript,javascript,html,canvas,infinite-loop,Javascript,Html,Canvas,Infinite Loop,其主要思想是尽可能快地永远循环函数,例如: function tick(){ callAnotherFunction(); // waiting for callAnotherFunction() to finish... // ...oh it's done -> call tick() again /* I tried to call tick(); before and after the closing curly bracket,

其主要思想是尽可能快地永远循环函数,例如:

function tick(){

    callAnotherFunction(); 

    // waiting for callAnotherFunction() to finish...
    // ...oh it's done -> call tick() again

    /* I tried to call tick(); before and after the closing curly bracket,
    but on the inside it leads to "Maximum call stack size exceeded"-ERROR
    and on the outside it never calls the function again. */
}

在jQ中,您可以使用when和then


如果没有jQ,您可以在另一个函数的末尾添加“return1”,然后将此结果放入变量。然后,通过一个简单的ifvariable{tick;}

进行递归调用,函数tick将一直调用自己,并且永远不会超过这一点,为了进行递归,您需要定义退出条件,只要满足该条件,递归就会停止,实际执行就会开始:

var i=0;
function tick(){

callAnotherFunction(); 

waiting for callAnotherFunction() to finish...
...oh it's done -> call tick() again
If(i++ <100) // this our exit condition, recursion stops whenever i => 100
   tick(); 

}

用谷歌做一些研究——这是一个众所周知的问题,有着简单的解决方案。实际上,计时器会停止,运行函数,然后重新启动。请使用setTimeout或requestAnimationFrame(最好是requestAnimationFrame)。setTimeout、setInterval或requestAnimationFrame都不会在函数最后一次完成时运行函数。例如,假设我的函数执行时间为2ms,我将超时时间设置为16ms,即14毫秒,什么都不做。