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
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because it’s not recursive. The
test2function is able to return, and some time later another invocation is scheduled bysetTimeoutvia the anonymous function that was created.Obviously, you can’t go back in time.
setTimeouthas a minimum duration.FWIW, the anonymous function is unnecessary. You could do
setTimeout(test2, -500).