我正在利用魔力jQuery.ajax( settings ).
但是,我想知道是否有人玩超时设置多吗?
我知道它基本上用于指示请求的本地时间,但如果达到超时,它是否可以触发任何内容?或者它只是停止听取回应?
阅读jQuery网站,我可以看到没有传递任何参数,所以它看起来像是一个具有一个功能的简单设置.哪个好.
但是,如果达到超时,我想触发警报或某些功能.在这种情况下,我可以看到错误设置没有被触发.
这是我的片段:
$("form#testform").submit(function(){
var allFormValues = $("form#testform").serialize();
$.ajax({
cache:false,
timeout:8000, // I chose 8 secs for kicks
type:"POST",
url:"someurl.php",
data:allFormValues,
error:function(){ alert("some error occurred") },
success:function(response){ alert(response); }
});
});
Run Code Online (Sandbox Code Playgroud)
有谁知道如何使用超时工作更多?
我们有一个需要每秒更新的显示板.对服务器的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 …Run Code Online (Sandbox Code Playgroud)