There is such a function:

var animateTitle = function(countMessages) { } 

How to re-call it in setTimeout ? Something like in the body of the function itself (recursion):

 setTimeout(animateTitle(call), 3000); 

Only parameters how to transfer correctly? In my example, an error occurs in the console:

Uncaught RangeError: Maximum call stack size exceeded

    2 answers 2

    Most likely, you'd better use not setTimeout, but setInterval. But if you really want, then call:

     function animateTitle(countMessages) { setTimeout(function(){ animateTitle(call); }, 3000); } 

    Just keep in mind that a callstack will happen. Through 7000-49000 iterations.

    • @Oleg Ponomarchuk, you need to call a function in the callback of the last action. - Deonis
    • @knes doesn't setTimeout and setInterval in this case have a critical difference? setTimeout will not make the interval between calls, because does not stop execution like sleep in other languages. Or do? - moron
    • He will not pause. He seems to create another branch of execution. Those. if the function itself is executed 4 seconds, we will accumulate a bunch of executable functions. BUT. As a rule, when you need to call a function from itself by timeout, people need either a timer (setInterval) or> callback of the last action. - knes
     setTimeout(animateTitle, 3000, call); // call - передаваемый параметр