js几种倒计时实现方式

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)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值