Can I have multiple window.setTimeout calls?

Discussion in 'JavaScript' started by sarahk, Aug 13, 2005.

  1. #1
    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:
     
    sarahk, Aug 13, 2005 IP
  2. Kings

    Kings Peon

    Messages:
    37
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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.
     
    Kings, Aug 13, 2005 IP
  3. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    J.D., Aug 13, 2005 IP
  4. TommyD

    TommyD Peon

    Messages:
    1,397
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I thought you could assign timeouts to variables, and have more than one.

    Might want to test if you are interested.

    hth,

    tom
     
    TommyD, Aug 13, 2005 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,807
    Likes Received:
    4,534
    Best Answers:
    123
    Trophy Points:
    665
    #5
    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!
     
    sarahk, Aug 13, 2005 IP
  6. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
     
    J.D., Aug 13, 2005 IP