// 抽出定时器函数 var fn = function () { // 开始时间 var s = new Date(); // 结束时间 var e = new Date(2019, 3, 18, 18, 27, 40); // 计算时间差 var timeObj = getDiff(s, e);
if (timeObj.temp <= 0) { // 停止定时器 clearInterval(flag); hours.innerText = '00'; minutes.innerText = '00'; seconds.innerText = '00'; } else { // 设置显示倒计时元素的内容 hours.innerText = timeObj.xiaoShi; minutes.innerText = timeObj.fenZhong; seconds.innerText = timeObj.miao; }
*/ function getDiff(start, end) { // 总毫秒差 var temp = end - start; // 小时 var hours = parseInt(temp / 1000 / 60 / 60); hours = hours < 10 ? '0' + hours : hours; // 分钟 var minutes = parseInt(temp / 1000 / 60 % 60); minutes = minutes < 10 ? '0' + minutes : minutes; // 秒 var seconds = parseInt(temp / 1000 % 60); seconds = seconds < 10 ? '0' + seconds : seconds; // 返回多个值 return {xiaoShi:hours,fenZhong:minutes,miao:seconds,temp:temp}; }