看到了有人问,回答了一下,然后记录在这里。修改JS
<script>
var jform = $('#form');
var jsubmit = $('#submit');
var jsend = $('#send');
var countdownInterval = null; // 保存定时器 ID
function startCountdown(duration) {
var endTime = Date.now() + duration * 1000; // 计算结束时间戳
localStorage.setItem('sendCountdownEndTime', endTime); // 保存到 localStorage
jsend.prop('disabled', true); // 禁用按钮
if (countdownInterval) {
clearInterval(countdownInterval); // 清理上次倒计时
}
countdownInterval = setInterval(function() {
var remainingTime = Math.max(0, Math.round((endTime - Date.now())/ 1000)); // 剩余秒数
jsend.button('<?php echo lang('user_send_sucessfully');?> ' + remainingTime + ' ');
if (remainingTime <= 0) {
clearInterval(countdownInterval);
countdownInterval = null;
jsend.button('reset');
jsend.prop('disabled', false); // 启用按钮
localStorage.removeItem('sendCountdownEndTime'); // 清除
}
}, 1000);
}
jsend.on('click', function() {
jform.reset();
jsend.button('loading');
var postdata = jform.serialize();
$.xpost(jsend.attr('action'), postdata, function(code, message) {
if(code == 0) {
$.alert(message);
$('#code').focus();
var t = 300; // 倒计时
jsend.button('<?php echo lang('user_send_sucessfully');?> 300 ');
jsubmit.button('reset');
startCountdown(t);
} else if(code < 0) {
$.alert(message);
jsend.button('reset');
} else {
jform.find('[name="'+code+'"]').alert(message).focus();
jsend.button('reset');
}
});
return false;
});
jform.on('submit', function() {
jform.reset();
jsubmit.button('loading');
var postdata = jform.serialize();
$.xpost(jform.attr('action'), postdata, function(code, message) {
if(code == 0) {
$.alert(message);
jsubmit.button(message).delay(1000).location(xn.url('user-resetpw_complete'));
} else if(xn.is_number(code)) {
alert(message);
jsubmit.button('reset');
} else {
jform.find('[name="'+code+'"]').alert(message).focus();
jsubmit.button('reset');
}
});
return false;
});
$(document).ready(function() {
var savedEndTime = localStorage.getItem('sendCountdownEndTime');
if (savedEndTime) {
var endTime = parseInt(savedEndTime);
var remainingTime = Math.max(0, Math.round((endTime - Date.now())/ 1000));
if (remainingTime > 0) {
startCountdown(remainingTime)
}
else{
jsend.prop('disabled', false);
}
}else{
jsend.prop('disabled', false);
}
});
</script>