如何在完成后重启功能 - Javascript

时间:2016-06-02 20:17:13

标签: 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,
    but on the inside it leads to "Maximum call stack size exceeded"-ERROR
    and on the outside it never calls the function again. */
}

2 个答案:

答案 0 :(得分:0)

你正在做一个递归调用,函数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(); 

}

答案 1 :(得分:-1)

在jQ中你可以使用when()和then()

https://api.jquery.com/jquery.when/

如果没有jQ,您可以在Another函数的末尾添加'return 1',然后将此结果放入变量。然后它将通过一个简单的if(变量){tick();}