1、10分钟倒计时
var time; var djs = document.getElementById("djs"); // var h = 23; var m = 10; var s = 00; time = setInterval(run, 1000); //进行倒计时显示 function run() { //停止计时器 if (s == 0 && m == 0) { clearInterval(time); return false; } --s; if (s < 0) { --m; s = 59; } djs.innerHTML = (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s); }
2、倒计时到哪一天的哪一时间
function leftTimer(year,month,day,hour,minute,second){ var leftTime = (new Date(year,month-1,day,hour,minute,second)) - (new Date()); //计算剩余的毫秒数 var days = parseInt(leftTime / 1000 / 60 / 60 / 24 , 10); //计算剩余的天数 var hours = parseInt(leftTime / 1000 / 60 / 60 % 24 , 10); //计算剩余的小时 var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟 var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数 days = checkTime(days); hours = checkTime(hours); minutes = checkTime(minutes); seconds = checkTime(seconds); var str = ` <h2>开奖倒计时</h2> <span >${days}</span>天<span>${hours}</span>时<span>${minutes}</span>分<span>${seconds}</span>秒 `; document.getElementById("djs").innerHTML = str; } setInterval("leftTimer(2019,8,1,0,0,0)",1000); function checkTime(i){ //将0-9的数字前面加上0,例1变为01 if(i<10) { i = "0" + i; } return i; }