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. */
}
3
  • Do some research with Google - it's a well known problem with easy solutions. Essentially, the timer stops, runs the function and then restarts. Commented Jun 2, 2016 at 20:19
  • Use setTimeout or, preferably, requestAnimationFrame. Commented Jun 2, 2016 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. Commented Jun 2, 2016 at 20:30

2 Answers 2

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

}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.