vue中使用倒計時


短信驗證碼60s倒計時

 

<template>
  <div>
    <button v-if="show" @click="getCode">獲取驗證碼</button>
    <button v-if="!show">{{ times }}s</button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      times: 60,
      show:true
    }
  },
  methods: {
    getCode() {
      this.show = false
      this.timer = setInterval(()=>{
        this.times--
        if(this.times===0){
          this.show = true
          clearInterval(this.timer)
        }
      },1000)
    }
  }
}
</script>

 

十五分鍾倒計時

 

<template>
  <div>
    <p>{{minute}}:{{second}}</p>
  </div>
</template>

<script type="text/ecmascript-6">

export default {
    data () {
      return {
        minutes: 15,
        seconds: 0
      }
    },
    mounted () {
      this.add()
    },
    methods: {
      num: function (n) {
        return n < 10 ? '0' + n : '' + n
      },
      add() {
        var _this = this
        var time = window.setInterval(function () {
          if (_this.seconds === 0 && _this.minutes !== 0) {
            _this.seconds = 59
            _this.minutes -= 1
          } else if (_this.minutes === 0 && _this.seconds === 0) {
            _this.seconds = 0
            window.clearInterval(time)

          } else {
            _this.seconds -= 1
          }
        }, 1000)
      },
    },
    watch: {
      second: {
        handler (newVal) {
          this.num(newVal)
        }
      },
      minute: {
        handler (newVal) {
          this.num(newVal)
        }
      }
    },
    computed: {
      second: function () {
        return this.num(this.seconds)
      },
      minute: function () {
        return this.num(this.minutes)
      }
    }
}
</script>

<style></style>

 

五分鍾倒計時 結束之后會再加五分鍾倒計時 跳轉頁面不會重新開始

 

<template>
  <div>
    <button @click="toPh">點擊</button>
    <p>{{minute}}:{{second}}</p>
  </div>
</template>

<script type="text/ecmascript-6">
export default {
    data () {
      return {
        minutes: '',
        seconds: '',
        tfcode: '',
        Num:''
      }
    },
    created () {
      this.TIme()
    },
    methods: {
      TIme (){ 
        var that = this
        this.Num =  localStorage.getItem('Num') 
        if(this.Num){
          let number = new Date()/1000 -that.Num
          if(that.Num != '' && number < 0){
            console.log('我還在五分鍾內')
            var minutes = parseInt(number*-1/60)
            var seconds = parseInt(number*-1)-minutes*60
            that.minutes = minutes
            that.seconds = seconds
            this.setTime()
          }else{
            console.log('我超過了五分鍾')
            let Num = Number(new Date()/1000)+(60*5)
            localStorage.setItem('Num',Num)
          }
        } else {
          this.setTime()
        }
      },
      setTime() {
        var that = this
        that.getTimer = setInterval(()=>{
          that.seconds-=1;
          if(that.seconds < 0){
            that.minutes-=1;
            that.seconds = 59
          }
          if(that.minutes == -1) {
            let Num = Number(new Date()/1000)+(60*5)
            localStorage.setItem('Num',Num)
            let number = new Date()/1000 - Num
            const minutes = parseInt(number*-1/60)
            const seconds = parseInt(number*-1)-minutes*60
            that.minutes = minutes
            that.seconds = seconds
          }
        },1000)
      },
      toPh() {
        this.$router.push({
          name: 'father'
        })
      },
      num: function (n) {
        return n < 10 ? '0' + n : '' + n
      },
    },
    watch: {
      second: {
        handler (newVal) {
          this.num(newVal)
        }
      },
      minute: {
        handler (newVal) {
          this.num(newVal)
        }
      }
    },
    computed: {
      second() {
        return this.num(this.seconds)
      },
      minute() {
        return this.num(this.minutes)
      }
    },
    beforeDestroy() {
      clearInterval(this.getTimer); //關閉
    },
}
</script>

<style lang="scss" scoped>
</style>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM