zoukankan      html  css  js  c++  java
  • 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>
    

     

  • 相关阅读:
    J2SE总结
    OSI模型与TCP/IP协议族
    poppler交叉编译
    摆脱技术思维,转向产品思维——寻找“万能”IDC的苦恼
    面向自由职业者和小型企业的开源开票工具
    3星|《中国做对了什么》:十几年前的文章集了,依旧不过时
    2星|《巴菲特致股东的信》:标题党,实际是1996年一次研讨会的发言记录,没有致股东的信
    3星|《不会被机器替代的人》:人在被服务的时候,更喜欢面对面跟人打交道,而不是跟机器打交道
    3星|《提高职场执行力》:执行力难关的根源是对话的无效性
    2星|《工业X.0》:物联网的资料汇编
  • 原文地址:https://www.cnblogs.com/woniu666/p/13339880.html
Copyright © 2011-2022 走看看