我正在尝试在ajaxsetup的beforesend函数中使用settimeout,但似乎发生的是ajax被发送,等待函数在超时后才被调用。我想在超时期间停止请求的发送。
jQuery.ajaxSetup({
beforeSend: function(){
setTimeout(continueExecution,1000)
return true;
}
});
有人可以提供一种停止从ajaxsetup发送请求的方法吗?
我正在尝试在ajaxsetup的beforesend函数中使用settimeout,但似乎发生的是ajax被发送,等待函数在超时后才被调用。我想在超时期间停止请求的发送。
jQuery.ajaxSetup({
beforeSend: function(){
setTimeout(continueExecution,1000)
return true;
}
});
有人可以提供一种停止从ajaxsetup发送请求的方法吗?
$.ajaxSetup({
beforeSend: function(jqXHR, options) {
setTimeout(function() {
// null beforeSend to prevent recursive ajax call
$.ajax($.extend(options, {beforeSend: $.noop}));
}, 5000);
return false;
}
});
你可以尝试将ajax请求放入setTimeout函数中。
setTimeout(function(){
//do the ajax request
}, 2000);
good luck!
beforeSend 没有问题。原因在于setTimeout是异步的。如果您尝试下面的代码,您会发现这一点,但繁忙等待可能不是一个好主意。
jQuery.ajaxSetup({
beforeSend: function() {
var i = 0;
function testt() {
i++;
console.log(i);
if (i < 10000) {
testt();
}
}
testt();
return true;
}
});