我有一个在后台运行的ajax。
var sTimeOut = function () {
$.ajax(
{
type: "POST",
url: '@Url.Action("checkJobIDs")',
dataType: "json",
async: true,
success: function (data) {
}
});
}
var interval = 10000;
setInterval(sTimeOut, interval);
搜索我想停止在背景中运行的另一个ajax的元素。
$.ajax(
{
type: "POST",
url: '@Url.Action("searchData")',
dataType: "json",
mtype: "post",
data: { str: str },
async: true,
complete: function () { $(sTimeOut).hide(); },
success: function (data) {
search(data.searched[0]);
}
});
在第二个AJAX工作时如何防止第一次AJAX?
您可以abort
请求。
您可以声明一个对象以保持当前执行请求,并检查是否正在运行搜索。如果搜索正在运行,请启动另一个reuquest会跳过。
var request = {
xhr: {},
isSearching: false
};
var sTimeOut = function () {
if(!request.isSearching) // prevent to run other request when is searching
{
request.xhr = $.ajax(
{
type: "POST",
url: '@Url.Action("checkJobIDs")',
dataType: "json",
async: true,
success: function (data) {
}
});
}
}
var interval = 10000;
setInterval(sTimeOut, interval);
搜索何时中止请求并阻止执行Anthorts。搜索完成后,您可以在后台恢复请求。
$.ajax(
{
type: "POST",
url: '@Url.Action("searchData")',
dataType: "json",
mtype: "post",
data: { str: str },
async: true,
beforeSend: function() { // stop background process
request.isSearching = true;
request.xhr.abort()
},
complete: function () { // resume background process
request.isSearching = false;
},
success: function (data) {
search(data.searched[0]);
}
});