简体   繁体   中英

strange behaviour in recursive function

Could someone explain why the second function doesn't bring us a stack overflow?

//stack overflow on call
function test1() {
    test1();
}
//no stack overflow, nor beer
function test2() {
    setTimeout(test2, -500); //back to the future
}

Because it's not recursive. The test2 function is able to return, and some time later another invocation is scheduled by setTimeout via the anonymous function that was created.

Obviously, you can't go back in time. setTimeout has a minimum duration.


FWIW, the anonymous function is unnecessary. You could do setTimeout(test2, -500) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM