Hello! I ask you to explain how the recursive setTimeout works, why the stack does not preserve the context of previous calls, as in the usual recursive function?

for example here:

var i = 1; setTimeout (function run () {func (i); setTimeout (run, 100);}, 100);

  • Tell me more, it is not clear what is wrong. Variable with function passed: jsfiddle.net/e9L63mp1 - Mr_Epic
  • one
    Give an example, expectation and reality. - D-side
  • because it's not really a recursion, - Grundy
  • Your example has worked for me, without calling func course, because you did not provide this function - Invision
  • There is no recursion in your example, there is only call scheduling after 100ms. Those. your code is executed immediately, and after 100ms the js engine will call the handler. - Vladimir Gamalyan

1 answer 1

Recursion Example with setTimeout

 (function loops(){ setTimeout(function(){ console.log('test'); loops(); // рекурсия }, 1000); })(); 

Demo http://codepen.io/anon/pen/revEqd?editors=1111