作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用示例中提到的ajaxQueue Queueing something like Ajax Calls :
// jQuery on an empty object, we are going to use this as our queue
var ajaxQueue = $({});
$.ajaxQueue = function( ajaxOpts ) {
// Hold the original complete function.
var oldComplete = ajaxOpts.complete;
// Queue our ajax request.
ajaxQueue.queue(function( next ) {
// Create a complete callback to fire the next event in the queue.
ajaxOpts.complete = function() {
// Fire the original complete if it was there.
if ( oldComplete ) {
oldComplete.apply( this, arguments );
}
// Run the next query in the queue.
next();
};
// Run the query.
$.ajax( ajaxOpts );
});
};
我还有一个函数来进行 Ajax 调用并将结果附加到 div(简化):
function ajaxCall() {
$.ajaxQueue({
type: "POST",
url: myURL,
async: true,
cache: false,
success: function( result ) {
$('#divID').append($('<div/>').html($(result).html()).fadeIn(200));
}
});
}
然后在点击事件上我循环执行 ajax 调用(简化):
$("#btn").on("click", function() {
// ( ??? ) Dequeue the ajaxQueue
$('#divID').html(''); // Clear current results
for(var i=0; i<15; i++) {
ajaxCall();
}
});
问题
如果用户在队列仍在运行时单击链接,则会添加一个新的 ajax 调用队列,从而产生比预期更多的结果。在新循环开始之前,我需要在单击时清除队列。
这是一个jsFiddle Demo 。非常感谢任何建议。
最佳答案
使用clearQueue :
ajaxQueue.clearQueue();
编辑
问题是可能仍然有一个 ajax 请求被调用。
因此您可能想要跟踪当前请求:
currentRequest = $.ajax( ajaxOpts );
并在清除队列时中止此操作:
if (currentRequest) {
currentRequest.abort();
}
关于jQuery,使 ajaxQueue 出列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16775447/
我使用以下代码对 ajax 请求进行排队,而不是阻止页面上的操作。问题是,我需要显示一个加载图标来指示队列的执行尚未完成。我不知道该怎么做,我尝试过的所有内容都立即关闭。 // jQuery on
我正在使用示例中提到的ajaxQueue Queueing something like Ajax Calls : // jQuery on an empty object, we are going
Corey Frang 在 stackoverflow 上发布了一个有用的插件,用于对 ajax 请求进行排队。就我而言,它对于以“ block ”的形式加载大量数据非常有用,从而最大限度地减少用户感
我是一名优秀的程序员,十分优秀!