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 DevGuru (normally really good)
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.
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); Code (javascript): J.D.
I thought you could assign timeouts to variables, and have more than one. Might want to test if you are interested. hth, tom
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> HTML: and it seems ok, so back to the drawing board on problem identification!
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> Code (javascript): 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.