最近在切专题页面,所以会遇到各种小需求,要搞各种小玩儿意儿,而且会重复用到,比如今天的倒计时,要定时到2019年x月x日的9点,那现在放代码。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div class="show">
<span id="time_d">00</span> 天
<span id="time_h">00</span> 时
<span id="time_m">00</span> 分
<span id="time_s">00</span> 秒
</div>
<script>
var getTime;
function show_time(){
// 设置定时器
getTime = setTimeout(show_time,1000);
var time_start = new Date().getTime(); //设定当前时间
var time_end = new Date('2019/10/1 00:00:00').getTime();
// 计算时间差
var time_distance = time_end - time_start;
// 天
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;
}
// 显示时间
$("#time_d").html(int_day);
$("#time_h").html(int_hour);
$("#time_m").html(int_minute);
$("#time_s").html(int_second);
}
</script>
</body>
</html>
应该有更简单的实现方法。嘿嘿