Home  >  Article  >  Web Front-end  >  php+js implements countdown function

php+js implements countdown function

黄舟
黄舟Original
2016-12-12 14:44:241225browse

js part

setInterval("time_controller()",1000);
function time_controller(){
    $(".time_val").each(function(){
         var time_id = "time_show_"+$(this).attr('for');
         var time_val= $(this).attr('value');
         $(this).attr('value',time_val-1);
         show_time(time_id,time_val*1000);
    });
}
function show_time(id,timestamp)
{
    var timer = document.getElementById(id);
    var str_time,int_day,int_hour,int_minute,int_second;
    var time_distance = timestamp;
    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;
          str_time="<b>剩余时间:"+int_day+"天"+int_hour+"小时"+int_minute+"分钟"+int_second+"秒</b>";
          timer.innerHTML=str_time;
    }else{
          timer.innerHTML="<b>剩余时间: 0天0小时0分钟0秒</b>";
    }
}


html part, time countdown for php and js

 <div class="time" id="time_show_{$list.cmd_id}"></div>
 <input type="hidden"  class="time_val" for="{$list.cmd_id}" value="{$list.time_val}" />


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn