SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How to show the count down time Immediately?

    when i access the page, the count down time(the red part under the Special Price: ) not load Immediately.it run after some time. how to eliminate the delay time. thank you.

    i use javascript onload
    (window.onload = function() {)
    event, but it maybe can't effect. i don't know why?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by runeveryday View Post
    when i access the page, the count down time(the red part under the Special Price: ) not load Immediately.it run after some time. how to eliminate the delay time. thank you.

    i use javascript onload event, but it maybe can't effect. i don't know why?
    The onload event waits until everything on the page has finished loading, which is why you experience the long delay.

    You have your code at the end of the page, which is a good thing, so replace this existing code:

    Code:
    window.onload = function() {
        if(_h<10){$('time_h').innerHTML = "0"+_h+":";}else{$('time_h').innerHTML = _h+":";}
        if(_m<10){$('time_m').innerHTML = "0"+_m+":";}else{$('time_m').innerHTML = _m+":";}
        if(_s<10){$('time_s').innerHTML = "0"+_s;}else{$('time_s').innerHTML = _s;}
        _t = setTimeout('doTime()', 1000); 
    }
    with this, which removes the onload event, and uses a more correct way to call the setTimeout:

    Code javascript:
    if(_h<10){$('time_h').innerHTML = "0"+_h+":";}else{$('time_h').innerHTML = _h+":";}
    if(_m<10){$('time_m').innerHTML = "0"+_m+":";}else{$('time_m').innerHTML = _m+":";}
    if(_s<10){$('time_s').innerHTML = "0"+_s;}else{$('time_s').innerHTML = _s;}
    _t = setTimeout(doTime, 1000);

    and things will work better.

    You could even use a function that provides you with the leading zero, which helps to simplify the code to this:

    Code javascript:
    function leadingZero(value) {
        return (value < 10) ? '0' + value : value;
    }
    $('time_h').innerHTML = leadingZero(_h) + ":";
    $('time_m').innerHTML = leadingZero(_m) + ":";
    $('time_s').innerHTML = leadingZero(_s);
    _t = setTimeout(doTime, 1000);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Evangelist runeveryday's Avatar
    Join Date
    Jul 2009
    Posts
    437
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    many thanks it works like a charm.


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
  •