60s 倒计时,页面刷新不影响

<template>
    <div class="login">
             <van-form @submit="onSubmit">
                <van-cell-group inset>
                    <van-field
                    v-model="username"
                    name="手机号码"
                    label="手机号码"
                    placeholder="请输入手机号码"
                    :rules="[{ required: true, message: '请填写手机号码' }]"
                    />
                    <van-field
                    v-show="true"
                    v-model="Captcha"
                    center
                    clearable
                    label="短信验证码"
                    placeholder="请输入短信验证码"
                    :rules="[{ required: true, message: '请填写验证码' }]"
                    >
                      <template #button>
                        <van-button round plain size="small" type="primary" @click="send" v-show="data.codeShow">发送验证码</van-button>
                        <van-button round plain size="small" type="primary"  v-show="!data.codeShow" disabled >{{data.count}}秒后重试</van-button>
                      </template>
                    </van-field>
 
 
                </van-cell-group>
                <div style="margin: 16px;">
                    <van-button round block type="primary" native-type="submit">
                      提交
                    </van-button>
                </div>
            </van-form>
 
    </div>
</template>

<script>
import 'vant/es/toast/style'
import 'vant/es/notify/style'
import { ref,reactive } from 'vue';
import {getCaptcha} from '@/request/api/home.js'
import { showNotify } from 'vant';
 
 
export default {
  setup() {
    const username = ref('');  //手机号
    const Captcha = ref('');    //验证码
    const data = reactive({    
      codeShow: true,    //按钮显示
      count: '',    //倒计时
      timer: null,    //计时器
    })
     
    return {
      username,
      Captcha,
      data
    };
  },
  
 created(){   //刷新页面时重新取得计时并将计时丢给timeDown执行
    let EndTime= sessionStorage.getItem('EndTime');
    if(EndTime){
        this.timeDown(EndTime);
    }
  },
 
  methods:{
    async send(){
        let clicktime = new Date().getTime() + 60000;   //未来60秒,这里也毫秒为单位
		sessionStorage.setItem('EndTime', JSON.stringify(clicktime));  //存入sessionStorage
        let res = await getCaptcha(this.username)  
        if (res.data.code == 200) {
          showNotify({ type: 'success', message: '发送成功' });
          this.timeDown(clicktime)  //请求成功则触发timeDown并将时间携带过去
        }else{
          showNotify({ type: 'danger', message:res.data.message  });
        }
    },
    timeDown(time){
          if (!this.data.timer) {
          this.data.count = Math.ceil((JSON.parse(time) - new Date().getTime())/1000);  //取出计时
          this.data.codeShow = false;
          this.data.timer = setInterval(() => {
              if (this.data.count > 0) {
                this.data.count--;
              } else {
                this.data.codeShow = true;
                clearInterval(this.data.timer);  //清除计时器
                this.data.timer = null;
                sessionStorage.removeItem('EndTime')  //计时完后清除sessionStorage
              }
          }, 1000)
        }
 
    },
};
 
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值