Repeat/loop ajax 调用计时器 - 即使超时
Repeat/loop ajax call on a timer - even if timed out
我们有一个显示板需要每秒更新一次。对服务器的 AJAX 调用会触发存储过程,这是一个非常简单的 SELECT 语句,执行仅需几毫秒。
最初,由于网络延迟(或太阳黑子,或者谁知道是什么),这个 AJAX 进程有时会(偶尔,很少)超时。通过调整我们处理计时器的方式并将 timeout
设置为 0,我们得到了它,所以它现在 运行 稳定并且超时永远不会发生......但是。
话虽如此,我仍然担心超时 可能 仍然会发生。如果发生,我们的目标是它会继续下去。基本上,忽略超时,然后再试一次......永远。不像 MaxError、RetryLimit 或 TryCount 等
这是我现在拥有的:
setTimeout(function run() {
// When the timer elapses, get the data from the server
GetData();
setTimeout(run, _refreshRate);
}, 1000);
function GetData() {
//console.log("Attempting to obtain the data...");
jQuery.ajax({
url: "something.ashx",
type: "GET",
contentType: 'application/json; charset=utf-8',
success: function(resultData) {
//console.log("Got the data.");
ParseJson(resultData);
// show the last refresh date and time
$('#refreshTime').html(GetDateTime());
},
error : function(xhr, textStatus, errorThrown) {
if (textStatus == 'timeout') {
//console.log("Timeout occured while getting data from the server. Trying again.");
// If a timeout happens, DON'T STOP. Just keep going forever.
$.ajax(this);
return;
}
},
timeout: 0,
});
}
ParseJson(resultData);
中的一切都很好用。那里没有问题。计时器已设置(我相信),因此它将等到一个 GetData() 完成后再尝试启动另一个。
我认为将 timeout
设置为 0
意味着 "don't ever time out."
我的问题是:
我是否正确处理了 error
超时?我正在使用此线程中的选定答案作为指导:
What's the best way to retry an AJAX request on failure using jQuery?
但我不需要 retryLimit 限制。
我也看过这些主题:
How to make the Ajax call again in case Time out error occurs
ajax timeout callback function
我 认为 我已将所有信息归结为一个简单的解决方案,但我想要一些同行评审。有更好的方法吗?
我更喜欢只在当前呼叫完成后才对新呼叫进行排队的解决方案。像..
function poll() {
setTimeout(function () {
GetData();
}, 1000);
}
function GetData() {
jQuery.ajax({
url: "something.ashx",
type: "GET",
contentType: 'application/json; charset=utf-8',
success: function(resultData) {
//...
},
error : function(xhr, textStatus, errorThrown) {
//...
},
complete: function() {
poll();
},
timeout: 0,
});
}
poll();
这样您的通话无论如何都不会有重叠的风险。
function GetData() {
//console.log("Attempting to obtain the data...");
jQuery.ajax({
url: "something.ashx",
type: "GET",
contentType: 'application/json; charset=utf-8',
success: function(resultData) {
//console.log("Got the data.");
ParseJson(resultData);
// show the last refresh date and time
$('#refreshTime').html(GetDateTime());
},
error : function(xhr, textStatus, errorThrown) {
if (textStatus == 'timeout') {
//console.log("Timeout occured while getting data from the server. Trying again.");
// If a timeout happens, DON'T STOP. Just keep going forever.
$.ajax(this);
return;
}
},
timeout: 0,
});
}
var myInterval = setInterval(getData, 1000)
// if you want to stop it elsewhere:
// clearInterval(myInterval)
您可以使用 setInterval
而不是超时
我们有一个显示板需要每秒更新一次。对服务器的 AJAX 调用会触发存储过程,这是一个非常简单的 SELECT 语句,执行仅需几毫秒。
最初,由于网络延迟(或太阳黑子,或者谁知道是什么),这个 AJAX 进程有时会(偶尔,很少)超时。通过调整我们处理计时器的方式并将 timeout
设置为 0,我们得到了它,所以它现在 运行 稳定并且超时永远不会发生......但是。
话虽如此,我仍然担心超时 可能 仍然会发生。如果发生,我们的目标是它会继续下去。基本上,忽略超时,然后再试一次......永远。不像 MaxError、RetryLimit 或 TryCount 等
这是我现在拥有的:
setTimeout(function run() {
// When the timer elapses, get the data from the server
GetData();
setTimeout(run, _refreshRate);
}, 1000);
function GetData() {
//console.log("Attempting to obtain the data...");
jQuery.ajax({
url: "something.ashx",
type: "GET",
contentType: 'application/json; charset=utf-8',
success: function(resultData) {
//console.log("Got the data.");
ParseJson(resultData);
// show the last refresh date and time
$('#refreshTime').html(GetDateTime());
},
error : function(xhr, textStatus, errorThrown) {
if (textStatus == 'timeout') {
//console.log("Timeout occured while getting data from the server. Trying again.");
// If a timeout happens, DON'T STOP. Just keep going forever.
$.ajax(this);
return;
}
},
timeout: 0,
});
}
ParseJson(resultData);
中的一切都很好用。那里没有问题。计时器已设置(我相信),因此它将等到一个 GetData() 完成后再尝试启动另一个。
我认为将 timeout
设置为 0
意味着 "don't ever time out."
我的问题是:
我是否正确处理了 error
超时?我正在使用此线程中的选定答案作为指导:
What's the best way to retry an AJAX request on failure using jQuery?
但我不需要 retryLimit 限制。
我也看过这些主题:
How to make the Ajax call again in case Time out error occurs
ajax timeout callback function
我 认为 我已将所有信息归结为一个简单的解决方案,但我想要一些同行评审。有更好的方法吗?
我更喜欢只在当前呼叫完成后才对新呼叫进行排队的解决方案。像..
function poll() {
setTimeout(function () {
GetData();
}, 1000);
}
function GetData() {
jQuery.ajax({
url: "something.ashx",
type: "GET",
contentType: 'application/json; charset=utf-8',
success: function(resultData) {
//...
},
error : function(xhr, textStatus, errorThrown) {
//...
},
complete: function() {
poll();
},
timeout: 0,
});
}
poll();
这样您的通话无论如何都不会有重叠的风险。
function GetData() {
//console.log("Attempting to obtain the data...");
jQuery.ajax({
url: "something.ashx",
type: "GET",
contentType: 'application/json; charset=utf-8',
success: function(resultData) {
//console.log("Got the data.");
ParseJson(resultData);
// show the last refresh date and time
$('#refreshTime').html(GetDateTime());
},
error : function(xhr, textStatus, errorThrown) {
if (textStatus == 'timeout') {
//console.log("Timeout occured while getting data from the server. Trying again.");
// If a timeout happens, DON'T STOP. Just keep going forever.
$.ajax(this);
return;
}
},
timeout: 0,
});
}
var myInterval = setInterval(getData, 1000)
// if you want to stop it elsewhere:
// clearInterval(myInterval)
您可以使用 setInterval
而不是超时