JS加PHP動態倒計時(定時器)

1.html

 <div class="box">
    <span>距離活動結束還剩:</span>
    <div class="content">
     <input type="text" id="day_show"><input type="text" id="hour_show"><input type="text" id="minute_show"><input type="text" id="second_show"></div>
</div>

2.css

div.box{width:100%;padding:3px;margin:10px;}
div.box>span{color:#FB78AC;font-style:italic;font-size: 15pt;}
div.content{width:100%;margin:5px;padding:3px;}
input[type='text']{width:35px;height:35px;padding:5px 10px;margin:5px;border:1px solid #ffe5e5;}

3.js

<script type="text/javascript">
 $(function(){ 
    show_time();
}); 

function show_time(){ 
    var time_start = new Date().getTime(); //設定當前時間

    var time_end =  new Date('{$finistime}').getTime(); //設定目標時間
    // 計算時間差 
    var time_distance = time_end - time_start; 
    /*判斷活動是否結束*/
    if(time_distance<0){

        int_day=0;
        int_hour=0;
        int_minute=0;
        int_second=0;
    }else{
          // 天
    var int_day = Math.floor(time_distance/86400000) 
    time_distance -= int_day * 86400000; 
    // 時
    var int_hour = Math.floor(time_distance/3600000) 
    time_distance -= int_hour * 3600000; 
    // 分
    var int_minute = Math.floor(time_distance/60000) 
    time_distance -= int_minute * 60000; 
    // 秒 
    var int_second = Math.floor(time_distance/1000) 
    // 時分秒爲單數時、前面加零 
    if(int_day < 10){ 
        int_day = "0" + int_day; 
    } 
    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; 
    } 
    }

    // 顯示時間 
    $("#day_show").val(int_day); 
    $("#hour_show").val(int_hour); 
    $("#minute_show").val(int_minute); 
    $("#second_show").val(int_second); 
    // 設置定時器
    setTimeout("show_time()",1000); 
}
</script>
發佈了101 篇原創文章 · 獲贊 25 · 訪問量 26萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章