View Full Version : Can I have multiple window.setTimeout calls?
sarahk
Aug 13th 2005, 2:17 pm
I'm just updating ad scripts on a client site and they've got multiple window.setTimeout's being set as different ads rotate.
To start with they were using the same function names and that was causing problems.
But even with everything fixed only one will rotate which leads me to suspect that window.setTimeout can only be used once!
Can anyone verify that?
Places I've been hunting:
Mozilla docs (http://www.mozilla.org/docs/dom/domref/dom_window_ref115.html)
DevGuru (http://www.devguru.com/Technologies/ecmascript/quickref/win_settimeout.html) (normally really good)
Kings
Aug 13th 2005, 4:42 pm
I think you overwrite it, each time you use it. Just like window.load. You need to use a wrapper function to set it, just like addLoadEvent (http://simon.incutio.com/archive/2004/05/26/addLoadEvent).
J.D.
Aug 13th 2005, 7:03 pm
You can use setInterval instead. If you have to use setTimeout, use something like this:
function to()
{
for(var i = 0; i < f.length; i++)
(f[i])(i);
window.setTimeout(to, 1000);
}
var f = new Array(0);
f.push(alert);
f.push(alert);
f.push(alert);
window.setTimeout(to, 1000);
J.D.
TommyD
Aug 13th 2005, 8:14 pm
I thought you could assign timeouts to variables, and have more than one.
Might want to test if you are interested.
hth,
tom
sarahk
Aug 13th 2005, 11:38 pm
Hmm, thanks for the replies
went to test it out using the devguru script with changes
<html>
<head>
<script type="text/javascript">
function displayAlert1()
{
alert("The GURU sez hi!")
window.setTimeout("displayAlert1()",5000);
}
function displayAlert2()
{
alert("alternate message")
window.setTimeout("displayAlert2()",6000);
}
</script>
</head>
<body>
<form>
Click on the button.
<br>
After 5 seconds, an alert will appear.
<br>
<input type="button" onclick="setTimeout('displayAlert1()',5000)" value="Click Me">
<br>
<input type="button" onclick="setTimeout('displayAlert2()',6000)" value="Click Me">
</form>
</body>
</html> and it seems ok, so back to the drawing board on problem identification!
J.D.
Aug 14th 2005, 12:56 am
How setTimeout is scheduled is browser-specific. For example, if you try this code in FF, it will bring up two alerts immediately, but then will pop a new one only after you dismiss some or all of the existing alerts. IE will create just one at a time.
<script type="text/javascript">
window.setTimeout("alert(0)", 1000);
window.setTimeout("alert(1)", 1000);
window.setTimeout("alert(2)", 1000);
window.setTimeout("alert(3)", 1000);
</script>
Don't dismiss the alerts, but instead just move them to see what I mean. It appears that setInterval works the same way.
J.D.
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.