定时器 setInterval 的关闭问题

来源:互联网 发布:java 集合类 深入详解 编辑:程序博客网 时间:2024/06/13 21:22

最近写了一个手机发送验证码的功能。遇到定时器关闭的问题。
注意:使用定时器名字 = null解决不了。必须要 clearTimeout(timer) 将指定的定时器关闭,不然定时器一直会跑在内存中。

 //前端显示发送手机验证码 function frontSendMsg() {     var num = 60;     var timer = setInterval(function () {         num--;         console.log(num)         if (num <= 0) {             $("#getPhoneCode").html("重新发送");             isSendMsg = true;             clearTimeout(timer);             return;         }         $("#getPhoneCode").html(num + "s后重新发送");     }, 1000); }
原创粉丝点击