0

The main idea is to loop function forever as fast as possible, something like:

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. */
}
Phillip Chan
  • 993
  • 7
  • 17
xpoopx
  • 41
  • 1
  • 9
  • Do some research with Google - it's a well known problem with easy solutions. Essentially, the timer stops, runs the function and then restarts. – Peter Smith Jun 02 '16 at 20:19
  • Use setTimeout or, preferably, requestAnimationFrame. – ManoDestra Jun 02 '16 at 20:23
  • None of setTimeout, setInterval or requestAnimationFrame runs function as soon as it finishes the last time. For example let's say my function takes 2ms to execute and I've set my timeout to 16 ms. That's 14 milliseconds doing nothing. – xpoopx Jun 02 '16 at 20:30

2 Answers2

0

You are doing a recursive call, which the function tick() will keep calling itself forever and never get past this point, in order to do a recursion you need to define your exit condition, which whenever that condition is met the recursion stops and the actual execution begin:

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(); 

}
Shadi Shaaban
  • 1,670
  • 1
  • 10
  • 17
-1

In jQ you could use when() and then()

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

Without jQ you could add 'return 1' at the end of Another function and then put this result to variable. Then it's gonna by an easy if(variable){tick();}

bakowroc
  • 21
  • 4