欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JS倒计时实例_天时分秒

程序员文章站 2022-11-26 10:57:06
html代码:
距离结束时间:

html代码:

<div id="times_wrap" class="time_num">
 距离结束时间:
 <div class="time_w">
  <b id="times_d" class="time"> </b>天
  <b id="times_h" class="time"> </b>时
  <b id="times_m" class="time"> </b>分
  <b id="times_s" class="time"> </b>秒
 </div>
</div>

javascript代码:

<script type="text/javascript">
settimeout("show_time()",1000);
var time_wrap = document.getelementbyidx_x("times_wrap");
var time_d = document.getelementbyidx_x("times_d");
var time_h = document.getelementbyidx_x("times_h");
var time_m = document.getelementbyidx_x("times_m");
var time_s = document.getelementbyidx_x("times_s");

var time_end = new date("2012/10/29 08:00:00"); // 设定结束时间
time_end = time_end.gettime();

function show_time(){
 var time_now = new date(); // 获取当前时间
  time_now = time_now.gettime();
 var time_distance = time_end - time_now; // 结束时间减去当前时间
 var int_day, int_hour, int_minute, int_second;
 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;
  
  // 显示时间
  time_d.innerhtml = int_day;
  time_h.innerhtml = int_hour;
  time_m.innerhtml = int_minute;
  time_s.innerhtml = int_second;
  
  settimeout("show_time()",1000);
 }else{
  time_d.innerhtml = time_d.innerhtml;
  time_h.innerhtml = time_h.innerhtml;
  time_m.innerhtml = time_m.innerhtml;
  time_s.innerhtml = time_s.innerhtml;
 
  // cleartimeout(timerid)
 }
};
</script>

以上这篇js倒计时实例_天时分秒就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。