function verifyCodeBtnHandler(e, callback = () => {}, t = 60) {
const _this = e.currentTarget || e.target;
// 防止多次点击
const status = $(_this).data('status');
if (status != 1) {
$(_this).data('status', 1);
const tip = $(_this).data('tip');
const text = $(_this).text();
// 设置禁用状态
$(_this).attr('disabled', true);
// t 秒倒计时
let time = t;
let timer = null;
const reset = () => {
clearInterval(timer);
$(_this).text(text);
$(_this).attr('disabled', false);
$(_this).data('status', 0);
};
const fun = () => {
time--;
if (time < 0) {
reset();
} else {
$(_this).text(time + ' ' + (tip ? tip : 'seconds try'));
}
};
const start = () => {
timer = setInterval(fun, 1000);
fun();
};
callback({ start, reset });
}
}
使用
verifyCodeBtnHandler(e, ({ start, reset }) => {
//ajax发送邮件
$.ajax({
url: '/get',
type: 'GET',
dataType: 'json',
data: {},
//接收回传参数
success: function (res) {
start();
},
error: function (data) {
reset();
},
});
});
});