我有一个代码:
$(document).ajaxComplete(function() {
DoSomething();
});
function DoSomething() {
...
$.get(MyUrl, function() {
...
});
...
}但$.get循环ajaxComplete事件:(
存在某种方式,如何从我的网址获得超文本标记语言内容到变量来使用这个变量(查找类和内容),或者以某种方式,如何使用$.get禁用进程的ajaxComplete事件?
发布于 2020-01-18 18:02:03
您可以在ajaxComplete中检查请求的URL。如果是您在DoSomething中请求的URL,请不要再次调用DoSomething:
$(document).ajaxComplete(function(_, __, { url }) {
if (url === 'https://jsonplaceholder.typicode.com/posts/1') {
console.log("Recursion detected, don't do anything");
} else {
console.log('Calling DoSomething');
DoSomething();
}
});
function DoSomething() {
$.get('https://jsonplaceholder.typicode.com/posts/1', function() {
console.log('DoSomething done');
});
}
$.get('https://jsonplaceholder.typicode.com/posts/5');<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
还可以在传递给$.get的options对象上设置属性,并检查.ajaxComplete中是否存在该属性
$(document).ajaxComplete(function(_, __, { fromDoSomething }) {
if (fromDoSomething) {
console.log("Recursion detected, don't do anything");
} else {
console.log('Calling DoSomething');
DoSomething();
}
});
function DoSomething() {
$.get({
url: 'https://jsonplaceholder.typicode.com/posts/1',
fromDoSomething: true
}, function() {
console.log('DoSomething done');
});
}
$.get('https://jsonplaceholder.typicode.com/posts/5');<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
https://stackoverflow.com/questions/59799373
复制相似问题