Calling function recursively at regular intervals

前端 未结 1 1387
梦毁少年i
梦毁少年i 2021-02-08 09:48

So I was wondering what is a better way (in terms of stack growth and performance) to recursively call a function at regular intervals? For example, lets say I want to read file

相关标签:
1条回答
  • 2021-02-08 10:30

    There is no recursion in the following simplified example:

    function test()
    {
       console.trace();
       setTimeout(test, 1000);
    }
    
    test();
    

    output (note that stack is not growing)

    Trace
        at test (/private/tmp/rec.js:3:12)
        at Object.<anonymous> (/private/tmp/rec.js:7:1)
        at Module._compile (module.js:449:26)
        at Object..js (module.js:467:10)
        at Module.load (module.js:356:32)
        at Function._load (module.js:312:12)
        at module.js:487:10
        at EventEmitter._tickCallback (node.js:238:9)
    Trace
        at Object.test [as _onTimeout] (/private/tmp/rec.js:3:12)
        at Timer.ontimeout (timers.js:101:19)
    Trace
        at Object.test [as _onTimeout] (/private/tmp/rec.js:3:12)
        at Timer.ontimeout (timers.js:101:19)
    Trace
        at Object.test [as _onTimeout] (/private/tmp/rec.js:3:12)
        at Timer.ontimeout (timers.js:101:19)
    Trace
        at Object.test [as _onTimeout] (/private/tmp/rec.js:3:12)
        at Timer.ontimeout (timers.js:101:19)
    Trace
        at Object.test [as _onTimeout] (/private/tmp/rec.js:3:12)
        at Timer.ontimeout (timers.js:101:19)
    Trace
        at Object.test [as _onTimeout] (/private/tmp/rec.js:3:12)
        at Timer.ontimeout (timers.js:101:19)
    
    0 讨论(0)
提交回复
热议问题