该插件依赖于jquery,用于发送短信验证码后的60秒倒计时:
下载完整代码: 下载
下载后的完整实例包括:
插件完整源码
countdown.js:
$(function(){
$(".captchaBtn").html('获取验证码').on("click", function(){
var that = $(this);
var seconds = 60;
that.attr("disabled", true);
that.html(seconds+'秒');
let promise = new Promise((resolve, reject) => {
let setTimer = setInterval(
() => {
seconds -= 1;
// console.info('倒计时:' + seconds);
that.html(seconds+'秒');
if (seconds <= 0) {
that.html('获取验证码');
resolve(setTimer)
}
}
, 1000)
})
promise.then((setTimer) => {
// console.info('清除');
clearInterval(setTimer);
that.attr("disabled", false);
})
})
});
如何使用
- 引入插件
<script src="jquery-2.1.1.min.js" type="text/javascript"></script>
<script src="countdown.js" type="text/javascript"></script>
需要依赖jquery
- 在"获取验证码"按钮上添加 class=“captchaBtn”
<button class="captchaBtn"></button>
ok,使用非常简单