验证码倒计时js


getVarify.js

// 验证码计时——第一种window.onload = function () { var send = document.getElementById(‘send‘), //按钮ID times = 10, // 别忘了改这里 timer = null; send.onclick = function () { // 计时开始 send.disabled = true; timer = setInterval(function () { times--; if (times <= 0) { send.value = ‘获取验证码‘; clearInterval(timer); times = 5; // 别忘了改这里 send.disabled = false; } else { send.value = times + ‘秒后重试‘ send.disabled = true; } console.log(times) }, 1000); // 发送请求获取验证码 console.log("sending...") }}// 验证码计时——第二种// 参数:倒计时秒数, 按钮jquery对象, 倒计时结束时显示的文字// 可以放到短信发送完毕后的回调函数里// switchMSG(60, $("#get-verify"), ‘获取验证码‘)function switchMSG(times, ele, txt) { ele.prop(‘disabled‘, true) var idT = setInterval(function() { if(times < 1) { ele.html(txt) ele.prop(‘disabled‘, false) clearInterval(idT) } else { ele.html(times+‘s‘) times-- } }, 1000)}

相关文章