Why is setTimeout() not firing?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aayybb
    New Member
    • Sep 2007
    • 7

    Why is setTimeout() not firing?

    Hi,
    I am not sure why setTimer is not working for me.

    I have the folowing fucntion before </head> and included few document.write to make sure it is working.
    [code=javascript]
    function adding(x1){
    document.write( "adding"+"< br/>");
    document.write( " x1="+x1);
    var x2=x1;
    document.write( " x2="+x2);

    if (x2 <= 10){
    x2=x2+1;
    document.write( " x2="+x2);
    setTimeout("add ing(x2)", 300);
    }else
    { document.write( "bye");
    }
    }
    [/code]
    Then I have the following after <body>
    [code=javascript]
    var x1=1;
    document.write( "x1="+x1);
    adding(x1);[/code]
    Last edited by pbmods; Sep 22 '07, 07:34 PM. Reason: Added CODE tags.
  • johnhjohn
    New Member
    • Dec 2006
    • 43

    #2
    The reason why the function is not working is because you are using document.write. Once that is called after the pages loads, the entire page is rewritten. Because of this, you will only see the increase once. I would recommend using alert() to make sure a function is working. In addition, instead of using
    [CODE=javascript]
    setTimeout("add ing(x2)", 300);
    [/CODE]

    Use this:
    [CODE=javascript]
    setTimeout("add ing("+x2+")", 300);
    [/CODE]

    I hope I helped.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, aayybb. Welcome to TSDN!

      Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).

      Please use CODE tags when posting source code:

      &#91;CODE=javas cript]
      JavaScript code goes here.
      &#91;/CODE]

      Comment

      • aayybb
        New Member
        • Sep 2007
        • 7

        #4
        Thank you very much. It works.



        Originally posted by johnhjohn
        The reason why the function is not working is because you are using document.write. Once that is called after the pages loads, the entire page is rewritten. Because of this, you will only see the increase once. I would recommend using alert() to make sure a function is working. In addition, instead of using
        [CODE=javascript]
        setTimeout("add ing(x2)", 300);
        [/CODE]

        Use this:
        [CODE=javascript]
        setTimeout("add ing("+x2+")", 300);
        [/CODE]

        I hope I helped.

        Comment

        • aayybb
          New Member
          • Sep 2007
          • 7

          #5
          Sorry. I will post a better message next time.



          Originally posted by pbmods
          Heya, aayybb. Welcome to TSDN!

          Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).

          Please use CODE tags when posting source code:

          [CODE=javascript]
          JavaScript code goes here.
          [/CODE]

          Comment

          Working...