javascript setInterval 和 Ajax
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18844365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setInterval and Ajax
提问by reyes
I have this problem when I use setInterval
and ajax for retrieving data from the database and if the data that I retrieve from the database is equal to saveHere then it will loop again until it does not match the variable saveHere, it freeze the browser until the data that I retrieve is not equal to saveHere.
当我使用setInterval
ajax 从数据库中检索数据时遇到这个问题,如果我从数据库中检索的数据等于 saveHere,那么它将再次循环,直到它与变量 saveHere 不匹配,它会冻结浏览器直到数据我检索的不等于 saveHere。
Here is an example:
下面是一个例子:
var saveHere = 'RED';
var interval = setInterval(function() {
var sample = $.ajax({
type: 'GET',
url: 'database.php',
data : data
}).responseText;
if (sample != 'RED') {
clearInterval(interval);
saveHere = sample;
}
else {
console.log('load again');
}
},1000);
I really need advice. Thank you in advance. Sorry for the grammar.
我真的需要建议。先感谢您。对不起语法。
回答by Znarkus
$.ajax
is asynchronous and requires you to use a callback to get the response text.
$.ajax
是异步的,需要您使用回调来获取响应文本。
Take a look at the documentation at http://api.jquery.com/jQuery.ajax/
查看http://api.jquery.com/jQuery.ajax/上的文档
What you want to do is to add a success
parameter. Something like this:
你想要做的是添加一个success
参数。像这样的东西:
var saveHere = 'RED';
doAjax();
function doAjax() {
$.ajax({
type: 'GET',
url: 'database.php',
data: data,
success: function (sample) {
if (sample != 'RED') {
saveHere = sample;
} else {
console.log('load again');
doAjax();
}
}
});
}
Notice how I've removed setInterval
and instead wrapped the Ajax code in a function. The success
callback will be called when the Ajax query has successfully finished, and give you the response. Once we have evaluated the response, we can run the doAjax
function again to run the query again.
请注意我是如何删除setInterval
Ajax 代码并将其包装在一个函数中的。在success
当阿贾克斯查询已成功完成回调将被调用,并给你的反应。一旦我们评估了响应,我们可以doAjax
再次运行该函数以再次运行查询。
回答by Chris Kempen
Without knowing the exact scenario, or what you're looking to achieve, I'd say the way you're going about your AJAX calls is very dangerous as it has the potential to constantly make a request every second, regardless of whether the server has had a chance to respond yet.
不知道确切的场景,或者你想要实现的目标,我会说你处理 AJAX 调用的方式是非常危险的,因为它有可能每秒不断地发出请求,无论服务器是否已经有机会回应了。
I'd be tempted to only make one request at a time, something like:
我很想一次只提出一个请求,例如:
var saveHere = 'RED';
makeAjaxCall();
function makeAjaxCall() {
var url = 'database.php';
var data = {};
$.get(url, data, function(response_text){
if (response_text != 'RED')
{
saveHere = response_text;
// do whatever else you need...
}
else
{
// make another call...
console.log('calling again...');
makeAjaxCall();
}
}, "text");
}
回答by Dipesh Parmar
You are clearing interval that means no interval will occur after that.
您正在清除间隔,这意味着此后不会发生间隔。
Instead what you can do is wrap interval inner code inside if condition as below.
相反,您可以做的是将间隔内部代码包装在 if 条件中,如下所示。
var interval = setInterval(function()
{
if(sample != 'RED') {
var sample = $.ajax({
type: 'GET',
url: 'database.php',
data : data
}).responseText;
saveHere = sample;
}
else
{
console.log('load again');
}
},1000);
回答by Pat Dobson
In your code you test for not equal to'RED'.
在您的代码中,您测试不等于“RED”。
if(sample != 'RED'){ . . .
That part of the loops stops the interval
循环的那部分停止间隔
If it doesn'tequal red
如果它不等于红色
}else{
It simple logs 'load again' withoutclearing the interval
它简单地记录“再次加载”而不清除间隔
What exactly are you trying to achieve ?
你到底想达到什么目的?