【发布时间】:2014-12-21 00:57:04
【问题描述】:
我有一个由 jQuery ajax 提交的表单,它有错误验证服务器端。在 beforeSend 我显示 一个 gif 加载器和一些加载文本,当验证发送回成功方法时,我会显示相应的错误消息。 此消息在 x 秒后有超时隐藏。无论如何,当我继续单击提交按钮时, setTimeout 本身就令人困惑,而以前的则没有清除。这是我正在使用的代码:
编辑
$('form').on('submit', function(e) {
e.preventDefault();
var timer = null;
$.ajax({
beforeSend; function() {
$('.response').html('Processing form, please wait...');
},
success: function(data) {
if(data.error == true) {
$('.response').html('An error occurred.');
} else {
$('.response').html('Thank you. Form submitted successfully.');
}
if(timer) { clearTimeout(timer) };
timer = setTimeout(function() {
$('.response').html('* Indicates required fields.');
}, 10000);
}
});
});
任何建议表示赞赏。
【问题讨论】:
标签: javascript jquery ajax settimeout cleartimeout