点击发送验证码(防止产生多个定时器)

简介: 点击发送验证码(防止产生多个定时器)
<div id="app">
   <div @click="goCode">
      {{shortMessage}}
   </div> 
</div>


<script>
new Vue({
    el: '#app',
    data() {
        return {
            timer: null,
            chishitime: 60,
            shortMessage: "获取手机验证码",
        };
    },
    methods: {
        goCode() {
            if (!this.timer) {
                this.shortMessage = "60s后重新发送"; //页面一开始就显示60s后重新发送.
                this.timer = setInterval(() => {
                    if (this.chishitime > 0) {
                        this.chishitime--;
                        console.log("定时器在执行");
                        this.shortMessage = this.chishitime + "s后重新发送";
                    } else {
                        clearInterval(this.timer);
                        this.shortMessage = "获取手机验证码";
                        this.timer = null;
                        this.chishitime = 60;
                    }
                }, 1000);
            }
        },
    },
    beforeDestroy() {
        clearInterval(this.timer);
    }
})
</script>


1425695-20200408231315902-239745641.gif

相关文章
|
人工智能 语音技术 云计算
基于客户真实使用场景的云剪辑Timeline问题解答与代码实操
本文为阿里云智能媒体服务IMS「云端智能剪辑」实践指南第6期,从客户真实实践场景出发,分享一些Timeline小技巧(AI_TTS、主轨道、素材对齐),助力客户降低开发时间与成本。
135833 86
基于客户真实使用场景的云剪辑Timeline问题解答与代码实操
|
机器学习/深度学习 编解码 人工智能
全球人类足迹数据(WSF)数据
全球人类足迹数据(WSF)数据
326 0
|
前端开发 JavaScript
原生实现环形进度条
原生实现环形进度条
123 0
|
设计模式 JavaScript 前端开发
JS 代码变量和函数的正确写法
JS 代码变量和函数的正确写法
117 3
|
JavaScript
[Vue]列表渲染(二)
[Vue]列表渲染(二)
[Vue]列表渲染(二)
|
缓存
Hudi Log日志文件读取分析(三)
Hudi Log日志文件读取分析(三)
133 0
|
数据采集 Python
Python爬虫:urlencode带参url的拼接
Python爬虫:urlencode带参url的拼接
431 0
|
API 网络架构
Node【七】初识Express框架
Node【七】初识Express框架
204 0
|
XML 缓存 Java
Spring 天天用,bean 懒加载原理你懂吗?
普通的bean的初始化是在容器启动初始化阶段执行的,而被lazy-init修饰的bean 则是在从容器里第一次进行context.getBean(“”)时进行触发。
Spring 天天用,bean 懒加载原理你懂吗?
|
IDE 开发工具
FastAPI(6)- 详解 Query (上)
FastAPI(6)- 详解 Query (上)
274 0
FastAPI(6)- 详解 Query (上)