[英]How to stop setTimeout that was started outside the function that detects if the form was submitted?
My setTimeout starts when the page is loaded, I need to stop it as soon as the user clicks the button and submit the form, the variable seems to get lost and I can't stop, any tips?我的 setTimeout 在页面加载时开始,我需要在用户单击按钮并提交表单时立即停止它,变量似乎丢失了,我无法停止,有什么提示吗?
my code我的代码
$(function() {
function atualiza(){
$.get('online.php?p=SMS',
function(resultado){
$('#onlineagora').html(resultado);
});
}
timer1 = setTimeout('atualiza()', 850);
$('#formsms').submit(function( event ) {
$.ajax({
url: 'aguardesms.php',
type: 'POST',
dataType: 'html',
data: $('#formsms').serialize(),
success: function(content){
clearTimeout(timer1);//STOP ATUALIZA
$("#DisplayDiv").html(content);
}
});
event.preventDefault();
});
});
solved using setInterval/clearInterval使用 setInterval/clearInterval 解决
var timer1 = setInterval(atualiza, 850);
function atualiza(){
$.get('online.php?p=SMS',
function(resultado){
});
}
$('#formsms').submit(function(e){
e.preventDefault();
clearInterval(timer1);
$.ajax({
url: 'aguardesms.php',
type: 'POST',
dataType: 'html',
data: $('#formsms').serialize(),
success: function(content){
$("#DisplayDiv").html(content);
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.