在vue中通过定时器制作短信验证码倒计时

背景:在项目中,我们有时候,经常会使用倒计时的功能,这次,我们来制作一个关于短信验证码倒计时的功能。

一、页面结构,然后,绑定点击事件

<div class="div-cell">
    <div class="line-cell">
        <label class="weui-label">短信验证码</label>
    </div>
    <div class="line-cell-bd">
        <input class="yzm-input1" type="text" v-model="PAY.CAPTCHA" input-limit="12" onkeyup="value=value.replace(/[^A-Za-z0-9]/g,'')">
        <i class="g-yzm1" @click="getMessCode()" v-show="currentResult.VcodeFlag == 1">获取验证码</i>
        <i class="g-yzm1 pay-yzm-s" v-show="currentResult.VCodeFlag == 2">{{currentResult.countDown}}</i>
        <i class="g-yzm1" @click="getMessCode()" v-show="currentResult.VCodeFlag == 3">重发验证码</i>
    </div>
</div>

     添加CSS样式,如下所示: 

.div-cell{
    position: relative;
    display: flex;
    background: #ffffff;
    border-bottom: 0.5px solid #ebebeb;
    margin: 0 15px;
}
.line-cell{
    margin: 0px 5px;
    padding: 14px 0px;
    height: 17px; 
}
.line-cell-bd {
    -webkit-box-flex: 1;
    flex: 1 1 0%;
    margin: 0px 5px;
    padding: 14px 0px;
    height: 17px;
}
.weui-label {
    display: block;
    width: 105px;
    overflow-wrap: break-word;
    word-break: break-all;
    color: rgb(45,50,61);
}
.yzm-input1 {
    padding-right: 90px;
    display: block;
    padding-top: 2px;
    padding-left: 0;
    color: #111;
    vertical-align: middle;
    font-size: 14px;
}
.g-yzm1 {
    width: 82px;
    height: 28px;
    line-height: 28px;
    border: 1px solid #356ae6;
    border-radius: 5px;
    color: #356ae6;
    font-size: 14px;
    text-align: center;
    right: 0px;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    z-index: 3;
}

二、js代码部分

export default{
  data(){
    currentResult:{
        codeInput: '', //图形验证码
        cardValue: '', 
        cardName: '',
        countDown: '获取验证码', //短信验证码
        VCodeFlag: 1,
        payTimedown: '', //支付倒计时
    },
    countdown: '120', //倒计时
    timer: null,
  },
  methods:{
    loading(){
       //启动定时器
       this.countdown--; //定时器减1
       this.currentResult.countDown = this.countdown + 's'; 
    },
    clearTimer(){
       //清除定时器
       clearInterval(this.timer);
       this.timer = null;
    },
    //获取短信验证码
    getMessCode(){
       /*获取验证码倒计时
         *VCodeFlag == 1 获取验证码
         *VCodeFlag == 2 倒计时
         *VCodeFlag == 3 重新获取验证码*/
        let that = this;
        var busi = {
            "PAY": that.PAY,
            "operateType": '1',
        }
        that.TOOL.apiAxios('POST','ZYT_TB_017',busi,function(status,res){
            if(status == '1'){
                 that.currentResult.VCodeFlag = 2;
                 that.countdown = 120;
                 that.currentResult.countDown = that.countdown + 's';
                 that.loading(); //启动定时器
                 that.timer = setInterval(() => {
                    //创建定时器
                    if(that.countdown === 0){
                         that.clearTimer(); //关闭定时器
                         that.currentResult.VCodeFlag = 3;
                    }else{
                       that.loading();
                    }
                 },1000);
                 MessageBox.alert('验证码已发送!','成功');
            }else if(status == '0'){
                 MessageBox.alert(res.error.msg,'错误');
            }
        })
    }
  },
}

相关图片如下所示:

 

总结:

       这样,就在vue项目中,实现了短信验证码倒计时的功能。

 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue实现验证码倒计时功能的关键是使用定时器来更新倒计时的时间,并根据时间的变化来控制按钮的状态和显示文本。以下是一个实现验证码倒计时功能的Vue代码示例: ```html <template> <button :disabled="isSend" @click="countDown">{{ codeName }}</button> </template> <script> export default { data() { return { isSend: false, codeName: "发送验证码", totalTime: 10, timer: null }; }, methods: { countDown() { if (this.isSend) return; this.isSend = true; this.codeName = this.totalTime + 's后重新发送'; this.timer = setInterval(() => { this.totalTime--; this.codeName = this.totalTime + 's后重新发送'; if (this.totalTime < 0) { clearInterval(this.timer); this.codeName = '重新发送验证码'; this.totalTime = 10; this.isSend = false; } }, 1000); } } }; </script> ``` 在上述代码,我们使用了一个`isSend`变量来控制按钮的禁用状态,`codeName`变量来控制按钮的显示文本,`totalTime`变量来表示倒计时的总时间,`timer`变量来保存定时器的引用。 在`countDown`方法,我们首先判断如果已经发送过验证码,则直接返回。然后将`isSend`设置为`true`,将`codeName`设置为倒计时的初始文本。接着使用`setInterval`函数创建一个定时器,每隔1秒更新一次倒计时的时间和按钮的显示文本。当倒计时结束时,清除定时器,将`codeName`恢复为重新发送的文本,将`totalTime`重置为初始值,将`isSend`设置为`false`。 这样,当用户点击按钮时,就会触发倒计时功能,按钮会进入禁用状态,并显示倒计时的时间。当倒计时结束后,按钮恢复为可点击状态,并显示重新发送的文本。 #### 引用[.reference_title] - *1* [vue实现验证码倒计时](https://blog.csdn.net/qq_41793354/article/details/121910500)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [vue实现验证码倒计时功能](https://blog.csdn.net/weixin_48168510/article/details/119873604)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值