SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Jul 2009
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A simple count down timer?

    Why isn't the setTimeout working?
    When I ran it, it displays all the numbers instantly.
    And, how do I do a clear screen before printing, so it doesn't append the results?
    http://jsfiddle.net/99QKp/1/
    Code:
    cd = {
        countDown: function(start) {
            var i = 0;
            while (i<start) {
              setTimeout(this.update(start), 1000);
              start--;
            }
            
        },
        update: function(start) {
          document.clear();
          document.write(start);
        }
    }
    cd.countDown(10);

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by resting View Post
    Why isn't the setTimeout working?
    Because you are executing the update function and assigning the returned value, which is undefined, to the timeout.
    Instead of that, you want to assign a function to the timeout instead.

    Code:
    setTimeout(function () {
        this.update(start), 1000);
    }
    The problem there now though is that the this keyword no longer refers to the same object, which is why we assign this to that, so that we can then use this.

    Code:
    var that = this;
    while (...) {
        setTimeout(function () {
        that.update(start), 1000);
    }
    The i variable doesn't ever change from 0, so get rid of the i variable and just use 0 instead.

    Code:
    var i = 0;
    while (start > 0) {
    Quote Originally Posted by resting View Post
    When I ran it, it displays all the numbers instantly.
    Because the scripting does not stop or pause with the setTimeout, we need to have the end of that update function call the countdown function again.
    That also means that the while statement must become an if statement, and the decrement needs to be moved elsewhere.

    Code:
    countDown: function(start) {
        ...
        whileif (start > 0) {
            ...
            start--;
        }
    },
    update: function(start) {
        ...
        start--;
        this.countDown(start);
    }
    ​
    Now the countdown occur at an appropriate delay.

    Quote Originally Posted by resting View Post
    And, how do I do a clear screen before printing, so it doesn't append the results?
    There is no "clear screen" command, but instead you can set the innerHTML of the body, which results in everything being replaced.

    Code:
    update: function(start) {
          document.body.innerHTML = start;
          start--;
          this.countDown(start);
        }
    }
    Last edited by paul_wilkins; Mar 29, 2012 at 20:55.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict
    Join Date
    Jul 2009
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that is awesome.
    finally it works http://jsfiddle.net/99QKp/3/
    thanks


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •