我有一个在后台运行的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);
在搜索时,请求中止并阻止执行另一个请求。搜索完成后,您可以继续在后台调用该请求。
$.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]);
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句