Leaving recursive functions running forever?

后端 未结 2 530
萌比男神i
萌比男神i 2021-01-25 10:36

I came across a function where it had a setTimeout inside with a timeout growing exponentially (timeout *= 2).

let timeout = 10000
fun         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-25 10:47

    This is not a recursive function call. The call to setTimeout will cause foo to be called by the JavaScript event loop at a later time.

    This code will not cause a stack overflow or any such problems. It should be completely safe.

    To understand how this works in-depth, I suggest reading up on the JS event loop and microtasks.

提交回复
热议问题