/    Sign up×
Community /Pin to ProfileBookmark

Recursion and the stack

Ok , I have this code, that refocuss a flash movie every ten milliseconds. It works, the function is called on loading the webpage and then calls itself recursively without a bottoming out clause. see below.
I need to know why i don’t get a stack overflow. Please help.
Everything I’ve read says I should get one. Its part of my thesis, and need to know why i don’t. Please help

function Refocus() {
window.document.movie.focus();
setTimeout(‘Refocus()’,10);
}

<BODY bgcolor=”#FFFFFF” onLoad=”window.document.movie.focus();Refocus();”>

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliMar 06.2005 — you are just setting focus back to the object ever X number of secs, to get a stack overflow you will need to put some thing in browsers memory and overload it thats when you will get an error.
Copy linkTweet thisAlerts:
@mr_d1982authorMar 06.2005 — Thanks,

i thought i was going mad for a while. and wondering why it didn't crash.

cheers for that great quick response. the best forum i've ever been on for a response this quick ?
Copy linkTweet thisAlerts:
@AdamGundryMar 06.2005 — To clarify slightly: setTimeout returns immediately, without waiting for the time to elapse. Therefore, you are not actually calling the function recursively, so as Khalid said the stack does not build up and overflow.

Incidentally, you might want to use setInterval instead of setTimeout for that.

Adam
Copy linkTweet thisAlerts:
@mr_d1982authorMar 06.2005 — Ok, I'm confused.. Calling a function from within the function is recursion, is that not right?

From what i read, setTimeout() waits until the specified timeframe is up and then runs the function. Is this not the way setTimeout works? Its probably ignorance but I don't see the advantage using setInterval().
Copy linkTweet thisAlerts:
@AdamGundryMar 06.2005 — Yes, calling a function within a function is recursion. However, what you are doing with setTimeout is not calling a function, it is scheduling a function to be called. The key difference being the old call to the function is gone by the time the new one occurs, and hence the stack is not used up. Sorry if I've confused you further.

I think setInterval might be clearer because you appear to be repeatedly calling the same function (every 10ms, or as near as JavaScript can do it), so a single setInterval outside the function would save having setTimeout inside it. Of course, you can keep the existing code if you want.

Adam
Copy linkTweet thisAlerts:
@mr_d1982authorMar 06.2005 — cheers, that really clears it up actually...

Now i know what i'm doing using setTimeout()....cheers...thanks for that.
×

Success!

Help @mr_d1982 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...