countdown timer

Discussion in 'JavaScript' started by izlik, Dec 11, 2007.

  1. #1
    I wonder if someone knows where i can get a countdown script for 24hours, or how one can be made? with active javascript

    basiclly what i need is, when 24hours of the set time has run, it resets to 0 and begins to count down from 24hours again... like if i put it on 09:00PM it should start counting down from there and reset when it reach 09:00Pm again the next day and start over...

    hope someone can help me.
     
    izlik, Dec 11, 2007 IP
  2. hrcerqueira

    hrcerqueira Peon

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Are you counting on having a page showing during the 24 hours?
    You can always use setInterval, or setTimeout.

    Imagine that you have a span element like this:
    
    <span id="timer"></span>
    
    Code (markup):
    a quick scratch, counting only hours, can look something like this:

    
    var timer = document.getElementById('timer');
    
    function startTimer() {
        window.clearInterval(interval);
    
           var currTime = new Date().getTime();
            var finishTime = new Date(currTime + 24 * 3600 * 1000).getTime();
    
        var interval = window.setInterval(function() {
            currTime = new Date().getTime();
            var dif = (finishTime - currTime) / 1000;
    
            if (dif <= 0) {
                startTimer();
                return;
            }
    
            var hours = parseInt(dif / 3600);
            var minutes = parseInt((dif % 3600) / 60);
            var seconds = parseInt((dif % 3600) % 60);
            document.body.innerHTML = hours + ':' + minutes + ':' + seconds;
        }, 500);
    }
    
    window.onload = startTimer;
    
    Code (markup):
     
    hrcerqueira, Dec 12, 2007 IP
  3. temp2

    temp2 Well-Known Member

    Messages:
    1,231
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    150
    Digital Goods:
    2
    #3
    good tut, thank you very much
    -----------
    setTimeout:
    Evaluates an expression after a specified number of milliseconds has elapsed
    In versions earlier than Microsoft® Internet Explorer 5, the first argument of setTimeout must be a string. Evaluation of the string is deferred until the specified interval elapses.

    As of Internet Explorer 5, the first argument of setTimeout can be a string or a function pointer.

    The specified expression or function is evaluated once. For repeated evaluation, use the setInterval method.

    When you use the setTimeout method with Introduction to DHTML Behaviors , the value of vCode should be a function pointer to call a function within the HTML Component (HTC) file or a string to call a function in the primary document.

    setInterval Method
    Evaluates an expression each time a specified number of milliseconds has elapsed.
    The setInterval method continuously evaluates the specified expression until the timer is removed with the clearInterval method.

    In versions earlier than Microsoft® Internet Explorer 5, the first argument of setInterval must be a string. Evaluation of the string is deferred until the specified interval elapses.

    As of Internet Explorer 5, the first argument of setInterval can be passed as a string or as a function pointer.

    To pass a function as a string, be sure to suffix the function name with parentheses. window. setInterval ("someFunction()", 5000);

    When passing a function pointer, do not include the parentheses. window. setInterval (someFunction, 5000);
     
    temp2, Dec 13, 2007 IP