/    Sign up×
Community /Pin to ProfileBookmark

setTimeout and setInterval Combination!

Hello!

I need to run a set interval after ‘x’ seconds. To do this I put the setInterval into a setTimeout, like this:

[CODE]setTimeout( “window.setInterval( ‘myFunction(“Dog”)’ ,5000)” ,5000 );
[/CODE]

But the web browser alert me that there is a ‘)’ missing!!!!

Then I try 2 make this in 2 steps like:

[CODE]setTimeout( “aFunction(“dog”)” ,5000 );

function aFunction (var)
{
window.setInterval( ‘myFunction(var)’ ,5000);
}
[/CODE]

But the web browser alert me that var is undefined!!

Anyone can help me please???May the first code works?? Thank you and have a good day.

Jonathan

to post a comment
JavaScript

16 Comments(s)

Copy linkTweet thisAlerts:
@rootMay 03.2007 — Why are you nesting event timers?

var is aslo a reserved word...
Copy linkTweet thisAlerts:
@JonathanDevauthorMay 03.2007 — Thank you for answer. 'var' is just an example, maybe i don't choose the better variable name ?. I want to nest event timers, because i want to make a repetitive task (setInterval) after a 'x' period (setTimeout).

What do you think?
Copy linkTweet thisAlerts:
@Mr_JMay 03.2007 — Try this

[code=php]onload=function test(){
setTimeout("aFunction('dog')",1000 );
}


function aFunction(v){
alert(v)
window.setInterval("aFunction('"+v+"')",5000);
}
[/code]
Copy linkTweet thisAlerts:
@felgallMay 03.2007 — To fix your original statement:

setTimeout( "window.setInterval( 'myFunction([b][/b]"Dog[b][/b]")' ,5000)" ,5000 );
Copy linkTweet thisAlerts:
@ricpMay 03.2007 — ricp's js annoyance list:

#1 using an evaluated string in a setTimeout/Interval.

setInterval(function, delay);

not

setInterval("function()",delay);



Not targeted at any person, just needed to be said.
Copy linkTweet thisAlerts:
@rootMay 03.2007 — ricp's js annoyance list:

#1 using an evaluated string in a setTimeout/Interval.

setInterval(function, delay);

not

setInterval("function()",delay);



Not targeted at any person, just needed to be said.[/QUOTE]


Either are acceptable and both have reasons to be...

you pass the objects reference if you have no arguments to pass or as a string if you have arguments to pass. You can even pass a string reference with parentheses and no arguments...
Copy linkTweet thisAlerts:
@felgallMay 03.2007 — It is more efficient to pass the function rather than a string where there are no parameters to be passed to the function.
Copy linkTweet thisAlerts:
@rootMay 03.2007 — But still, it doesnt answer why he is using a timeout to start a interval timer which will call the function in 5 seconds in any case.
Copy linkTweet thisAlerts:
@felgallMay 04.2007 — Because doing it that way means that there is a 10 second delay before the first execution of the function which then repeats at 5 second intervals. There is no other way I know of to set a different delay before the first time than there is before the subsequent ones.

To ask why you would want to do this shows that you don't understand the difference between setTimeout (run once after a delay) and setInterval (run multiple times with a delay in between each).
Copy linkTweet thisAlerts:
@rootMay 04.2007 — Yes I do know the difference, I just dont see any reason for it and FYI it will be 15 seconds and not 10.

Another method would be...function callback(arg){
if(arguments.length){ // if a parameter is set!
clearInterval(myTimer);
myTimer = setInterval(callback,arg-0);
return;
}

// other functions and processes

}// end of function
myTimer = setInterval('callback(5000)',5000);
sets the inital callback for 5 seconds and on first run it calls the function with a parameter then ignors first loop and then sets a new event timer to dun on from that every 5 seconds making first actions within the function body to run after 10 seconds.
Copy linkTweet thisAlerts:
@felgallMay 04.2007 — Well that is certainly a far more complicated way to do it - why put a setInterval in a loop and clear it each time when a setTimeout does the same thing without needing to be cleared. Why have a loop at all when a setInterval can handle the repetitions without a loop.. Also the last time I looked a five second delay followed by another five second delay was 10 seconds and not 15.
Copy linkTweet thisAlerts:
@rootMay 04.2007 — Duh it doesnt if I add the bit I missed out... The callback is called without that argument for subsequent call backs hence no resetting...
Copy linkTweet thisAlerts:
@rootMay 04.2007 — OR..function callback(arg){
if(arguments.length){ // if a parameter is set!
if(typeof myTimer!='undefined) clearInterval(myTimer);
myTimer = setInterval(callback,arg-0);
return;
}

// other functions and processes

}// end of function
callback(5000);
will now do it from a single function call with a parameter to set the ball rolling.
Copy linkTweet thisAlerts:
@felgallMay 04.2007 — if(typeof myTimer!='undefined[b]'[/b]) clearInterval(myTimer);
Copy linkTweet thisAlerts:
@rootMay 04.2007 — [function callback(arg){
if(arguments.length){ // if a parameter is set!
if(typeof myTimer!='undefined') clearInterval(myTimer);
myTimer = setInterval(callback,arg-0);
return;
}

// other functions and processes

}// end of function
callback(5000);

Wow, a typo a 3 year old could have corrected, I will now have to go n throw myself off a tall bridge! My Reputation is now in ruins. Cheers.
Copy linkTweet thisAlerts:
@rootMay 04.2007 — function callback(arg){
if(arguments.length){ // if a parameter is set!
if(typeof myTimer!='undefined') clearInterval(myTimer);
myTimer = setInterval(callback,arg-0);
return;
}

// other functions and processes

}// end of function
callback(5000);

Wow, a typo a 3 year old could have corrected, I will now have to go n throw myself off a tall bridge! My Reputation is now in ruins. Cheers.
×

Success!

Help @JonathanDev 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.19,
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,
)...