function startTime(arr, obj) {
var today = new Date(),
def = {
onlyOneDayTempHtml: 'HH:MM:SS',
dayTempHtml: '还有DD天'
},
obj = $.extend({}, def, obj),
format = function (str) {
if (parseInt(str) < 10) {
return "0" + str;
}
return str;
},
controller = function () {
setTimeout(controller, 1000)
today = new Date(today.getTime() + 1000)
for (var i = 0, j = arr.length; i < j; i++) {
var item = arr[i],
elem = $(item.elem),
value = item.value
var timeElem = new Date(value).getTime(),
timeold = timeElem - today.getTime(),
daysold = Math.floor(timeold / 1000 / 60 / 60 / 24),
hrsold = format(Math.floor(timeold / 1000 / 60 / 60 % 24)),
minsold = format(Math.floor(timeold / 1000 / 60 % 60)),
seconds = format(Math.floor(timeold / 1000 % 60))
if (daysold == 0) {
// 如果总倒计时间小于1天
var tempHtml = obj.onlyOneDayTempHtml.replace('HH', hrsold)
tempHtml = tempHtml.replace('MM', minsold)
tempHtml = tempHtml.replace('SS', seconds)
elem.html(tempHtml)
}
else {
if (daysold < 0) {
var tempHtml = obj.onlyOneDayTempHtml.replace('HH', '00')
tempHtml = tempHtml.replace('MM', '00')
tempHtml = tempHtml.replace('SS', '00')
elem.html(tempHtml)
} else {
var tempHtml = obj.dayTempHtml.replace('DD', daysold)
tempHtml = tempHtml.replace('HH', hrsold)
tempHtml = tempHtml.replace('MM', minsold)
tempHtml = tempHtml.replace('SS', seconds)
elem.html(tempHtml)
}
}
}
}
console.log(obj)
setTimeout(controller, 100)
}