1. 常见获取验证码60s倒计时
var second = 60;
var timer;
// 倒计时
function countDown(){
second--;
if(second == 0){
second = 60
clearTimeout(timer)
return
}
timer = setTimeout('countDown()', 1000);
},
countDown()
2. 天级倒计时只获取最高两个单位,即大于一天就显示 x天x时 大于一小时显示 x时x分 最后显示倒计时秒数
var timer,first,second,third,fourth;
var endTime // 结束时间时间戳
var nowTime // 当前时间戳
var end = endTime - nowTime
var days = end/1000/60/60/24
var daysRound = Math.floor(days)
var hours = end/1000/60/60 - (24*daysRound)
var hoursRound = Math.floor(hours)
var miuntes = end/1000/60-(24*60*daysRound)-(60*hoursRound)
var minRound = Math.floor(miuntes)
var seconds = end/1000-(24*60*60*daysRound)-(60*60*hoursRound)-(60*minRound)
var secRonund = Math.floor(seconds)
if(days > 1){
first = daysRound
second = '天'
third = hoursRound
fourth = '时'
} else if (hours > 1) {
first = hoursRound
second = '时'
third = minRound
fourth = '分'
} else if (miuntes > 1){
var restTime = minRound*60 + secRonund
timer = setInterval(function(){
if (restTime >= 0) {
var m = Math.floor(restTime / 60)
var s = Math.floor(restTime % 60)
first = m
second = '分'
third = s
fourth = '秒'
--restTime;
} else{
clearInterval(timer)
first = 00
second = '分'
third = 00
fourth = '秒'
}
},1000)
}
3. 天级倒计时显示全部倒计时
var timer,endTime
timer = setInterval(function(){
var time = endTime - (new Date().getTime())
if (time >= 0) {
var d = Math.floor(time/(1000*60*60*24))
d = d > 9 ? d : '0' + d
var h = Math.floor(time/1000/60/60%24)
h = h > 9 ? h : '0' + h
var m = Math.floor(time/1000/60%60)
m = m > 9 ? m : '0' + m
var s = Math.floor(time/1000%60)
s = s > 9 ? s : '0' + s
} else{
clearInterval(timer)
}
},1000)