/    Sign up×
Community /Pin to ProfileBookmark

How to tell when a recurvsive method has finished

What is the easiest way to know when a recursive method has finished executing, so that another method may be called?

I have the following code:

[code]
function myMethod() {
// some code

recursiveMethod();

// I want to wait 3 seconds after recursive method has finished before nextMethod() is invoked
// unfortunately, nextMethod() is invoked 3 seconds after recursiveMethod() is first called
timer = setTimeout(‘nextMethod();’, 3000);
}
[/code]

I am moving an object a distance which is equal to the size of the object being moved (via the recursiveMethod() ), and when it has moved the necessary distance, I want it to stay there for 3 seconds, and then move back to its original location (via nextMethod() ).

Depending on the size of the object, and given a constant speed, the method in setTimeout() may be invoked before the object has moved the necessary distance.

How can I know when recursiveMethod() has finished executing, so that I can invoke nextMethod() (via the setTimeout() method)?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliNov 04.2004 — you can get a value returned from the recursive method that when it fullfils all of its processing it finally returns a true/false value which u can use for further logic
Copy linkTweet thisAlerts:
@JuuitchanNov 05.2004 — Why not put the setTimeout() inside recursiveMethod() in such a way that it is invoked only if recursiveMethod() has finished its job?

Like this:

timer=fwrm?setTimeout('nextMethod();', 3000):0;

where fwrm is to be set to true when (and not until) we are finished with recursiveMethod().
Copy linkTweet thisAlerts:
@JPnycNov 05.2004 — What units will you be moving it by, each pass? Pixels? How many? Just figure out how many passes you need, and include a test of a variable which you increment each time through the routine. Then call the reverse function, or you could make it all in one function.
Copy linkTweet thisAlerts:
@wood_tahauthorNov 05.2004 — Thanks to all for your suggestions... I was able to accomplish what I needed.
×

Success!

Help @wood_tah 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.8,
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,
)...