settimeout problem...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sencomenco
    New Member
    • Nov 2006
    • 1

    settimeout problem...

    Hello,

    I have a problem with settimeout.
    When I call the initCountDown method on body's onLoad event. If I call the initCountDown on body's load, the button's code works fine afterwards.

    However, if I do not place initcountDown in body's load, the button calls countDown method only once and settimeout does not function.

    Any help or tips would be greatly appreciated.
    Thanks

    *************** *************** ***************
    [CODE=javascript]<SCRIPT language="JavaS cript">
    var secs;
    var timeout;
    function initCountDown(i nit) {
    secs = init;
    countDown();
    }

    function countDown () {
    if (secs >= 0) {
    self.status = secs;
    secs = secs - 1 ;
    timeout = setTimeout(func tion(){countDow n();}, 1000);
    } else {
    clearTimeout(ti meout);
    }
    }
    </SCRIPT>

    <body onload="initCou ntDown(10)">
    <form>
    <INPUT type=submit id=btnCount value="Count Down" onClick = "initCountDown( 10)">
    </form>
    </body>[/CODE]
    Last edited by gits; May 8 '08, 06:46 PM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    just add a return false; to the js-call for the button's onclick, otherwise the form is submitted (since your button is a submit-button) and the page reloaded:

    [CODE=javascript]<input type="submit" value="Count Down" onClick = "initCountDown( 10); return false;">
    [/CODE]
    another option would be to use type="button" for the button :) and submit the form through javascripts submit() method.

    kind regards

    Comment

    Working...