Javascript 定时AJAX请求
我正在使用jQuery,我有一个aAax请求,如下所示Javascript 定时AJAX请求,javascript,jquery,Javascript,Jquery,我正在使用jQuery,我有一个aAax请求,如下所示 $.ajax({ type: 'POST', url: 'test.php', data: { data: "test_data" }, cache: false, success: function(result) { if (result == "true"){
$.ajax({
type: 'POST',
url: 'test.php',
data: {
data: "test_data"
},
cache: false,
success: function(result) {
if (result == "true"){
alert("true");
}else{
alert("false");
}
},
});
这很有效。但是,我希望在特定的时间间隔内及时完成这项工作。我想每30秒执行一次。不应重新加载该页面。我希望这一切发生在幕后,隐藏起来。有人知道怎么做吗?使用setInterval
或者,如果您只希望它在成功消息之后运行,您可以在成功回调中使用setTimeout调用使用setInterval发送ajax请求的函数
setInterval(function() {
$.ajax({
type: 'POST',
url: 'test.php',
data: {
data: "test_data"
},
cache: false,
success: function(result) {
if (result == "true"){
alert("true");
}else{
alert("false");
}
}
});
}, 30000);
或者,如果您只希望它在成功消息后运行,则可以在成功回调中使用setTimeout调用发送该ajax请求的函数。只是不要提及W3学校,那里有很多错误。我知道,但我认为这不适用于这种情况,并且有一个非常基本但有用的总结:-)+1。只是不要提及W3学校,那里有很多错误。我知道,但我不认为这适用于这种情况,并且有一个非常基本但有用的总结:-)这不是我想要的。我希望我的脚本在一个循环中,每30秒初始化一次,不管结果如何,就像每30秒提醒“嗨”。修复!如果您想停止定时请求,只需存储setInterval返回并稍后销毁它。这不是我想要的。我希望我的脚本在一个循环中,每30秒初始化一次,不管结果如何,就像每30秒提醒“嗨”。修复!如果要停止定时请求,只需存储setInterval返回并稍后销毁即可。
setInterval(function() {
$.ajax({
type: 'POST',
url: 'test.php',
data: {
data: "test_data"
},
cache: false,
success: function(result) {
if (result == "true"){
alert("true");
}else{
alert("false");
}
}
});
}, 30000);