html
<span class="joinh">00</span>
<span class="joinm">00</span>
<span class="joins">00</span>
js
var time='2018-11-12 00:00:00';
var time1 = time.replace(/-/g,"/");裁剪字符串
timer(time1);
function timer(timeStr){
setInterval(function(){
let nowTime = new Date(timeStr) - new Date();//需求时间减去当前时间
let hours = parseInt(nowTime / ( 1000 * 60 * 60), 10); //计算剩余的小时
let minutes = parseInt(nowTime / 1000 / 60 % 60, 10);//计算剩余的分钟
let seconds = parseInt(nowTime / 1000 % 60, 10);//计算剩余的秒数
hours = checkTime(hours);
minutes = checkTime(minutes);
seconds = checkTime(seconds);
$(".joinh").html(hours);
$(".joinm").html(minutes);
$(".joins").html(seconds);
if( new Date(timeStr) < new Date()){ //判断需求时间是否小于当前时间
$(".joinh").html('00');
$(".joinm").html('00');
$(".joins").html('00');
clearTimeout(timer);
}
},1000);
}
function checkTime(i) { //将0-9的数字前面加上0,例1变为01
if (i < 10) {
i = "0" + i;
}
return i;
}