UNPKG

countdown-cbd-roywu

Version:

车巴达前端倒计时通用模块

63 lines (59 loc) 1.55 kB
let sid; let surplus = { hour: '00', minute: '00', second: '00' }; function countDownByDate(startTime = new Date().getTime(), endTime = new Date().getTime()) { if(typeof startTime == 'string') { startTime = new Date(startTime).getTime() } if(typeof endTime == 'string') { endTime = new Date(endTime).getTime() } let runTime = parseInt((endTime - startTime) / 1000); clearInterval(sid); sid = setInterval(() => { runTime = runTime - 1; surplus = calculateTime(runTime); }, 1000) } function countDownBySecond(runTime) { clearInterval(sid); sid = setInterval(() => { runTime = runTime - 1; surplus = calculateTime(runTime); }, 1000) } function calculateTime(runTime) { if (runTime < 0) { clearInterval(sid); return; } var hour = Math.floor(runTime / 3600); if (hour < 10) { hour = '0' + hour.toString(); } else { hour = hour.toString(); } runTime = runTime % 3600; var minute = Math.floor(runTime / 60); if (minute < 10) { minute = '0' + minute.toString(); } else { minute = minute.toString(); } runTime = runTime % 60; var second = runTime; if (second < 10) { second = '0' + second.toString(); } else { second = second.toString(); } return { hour, minute, second } } export { countDownByDate, countDownBySecond, surplus }