const formatTime = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}` } const formatNumber = n => { n = n.toString() return n[1] ? n : `0${n}` } const urlToObj = function(url) { let obj = {} let str = url.slice(url.indexOf('?') + 1) let arr = str.split('&') for(let i = 0 ; i < arr.length ; i ++) { let arr_temp = arr[i].split('=') obj[arr_temp[0]] = arr_temp[1] } return obj } const removeCss = function(content) { let reg=/(style|class)="[^"]+"/gi; let img=/]+>/gi; let res; if(img.test(content)) { res = content.match(img); for(let i=0;i { if(callBackList.length == 0) { console.log('倒计时结束 关闭定时器'); clearInterval(timer_); timer_ = null; } callBackList.forEach((item,index) => { item.currentTime-- var h = parseInt(item.currentTime / 3600); //小时 var m = parseInt(item.currentTime / 60 % 60); //分钟 var s = parseInt(item.currentTime % 60); //当前的秒 let obj = { h,m,s, currentTime:item.currentTime } item.fn(obj); if(item.currentTime <= 0) { callBackList.splice(index,1); } }); }, 1000); } } const addTime = function(callback,currentTime) { if(currentTime) { callBackList.push({fn: callback,currentTime}); console.log('第'+callBackList.length+'个倒计时开启'); if(!timer_) { startTimer() } } } const removeTime = function(fn) { for( var i = 0 ; i < callBackList.length ; i ++) { let item = callBackList[i]; if(item.fn == fn) { console.log('清除倒计时~'); callBackList.splice(i,1); } } } function toQueryPair(key, value) { if (typeof value == 'undefined') { return `${key}=&`; } return `${key}=${value}&`; } module.exports = { formatTime, urlToObj, removeCss, getPrePage, objToParam, addTime, removeTime }