js 计时器

实现功能:静态页实现自动显示距离某未来时间的倒计时,精确到秒

View Code
<div class="rmain time">
    <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" >
      <tr>
        <td>日</td>
        <td>&nbsp;</td>
        <td>时</td>
        <td>&nbsp;</td>
        <td>分</td>
        <td>&nbsp;</td>
        <td>秒</td>
        </tr>
      <tr>
        <td height="40"><span id="strd"></span></td>
        <td>:</td>
        <td><span id="strh"></span></td>
        <td>:</td>
        <td><span id="strm"></span></td>
        <td>:</td>
        <td><span id="strs"></span></td>
        </tr>
    </table>
    </div>   
    <script type='text/javascript' language="javascript">
    var time_now_server, time_now_client, time_end, time_server_client, timerID;
    time_now_server = new Date();
    var time_end = new Date("July 24 15:00:00  2012");//January 1 10:30:00  2009
    var df1 = time_end.getTime() - time_now_server.getTime();
    time_server_client = df1;
    function show_time() {
        var time_now, time_distance, str_time;
        var int_day, int_hour, int_minute, int_second;
        var time_now = new Date();
        time_now = time_now.getTime() + time_server_client;
        time_distance = time_server_client;
        time_server_client -= 1000;
        df1 -= 1000;
        if (time_distance > 0) {
            int_day = Math.floor(time_distance / 86400000);
            time_distance -= int_day * 86400000;
            int_hour = Math.floor(time_distance / 3600000);
            time_distance -= int_hour * 3600000;
            int_minute = Math.floor(time_distance / 60000);
            time_distance -= int_minute * 60000;
            int_second = Math.floor(time_distance / 1000);
            if (int_hour < 10)
                int_hour = "0" + int_hour;
            if (int_minute < 10)
                int_minute = "0" + int_minute;
            if (int_second < 10)
                int_second = "0" + int_second;
            document.getElementById("strd").innerHTML = int_day;
            document.getElementById("strh").innerHTML = int_hour;      //获取当前小时数(0-23)
            document.getElementById("strm").innerHTML = int_minute;    //获取当前分钟数(0-59)
            document.getElementById("strs").innerHTML = int_second;    //获取当前秒数(0-59)
            setTimeout("show_time()", 1000);
        }
        else {
            document.getElementById("strd").innerHTML = "0";
            document.getElementById("strh").innerHTML = "0";      //获取当前小时数(0-23)
            document.getElementById("strm").innerHTML = "0";    //获取当前分钟数(0-59)
            document.getElementById("strs").innerHTML = "0";    //获取当前秒数(0-59)   
            clearTimeout(timerID)
        }
    }
    setTimeout("show_time()", 1000);
</script>

posted on 2012-07-05 14:52  judy_ma0605  阅读(202)  评论(0编辑  收藏  举报

导航