获取最新的AJAX请求,并放弃其他 [英] Get latest ajax request and abort others

查看:139
本文介绍了获取最新的AJAX请求,并放弃其他的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找这个问题看似简单,但无法找到答案。我有多个请求,调用不同的URL。但对于每个URL,我只想要结果一次,它必须是最后一个在同一个URL被调用。我现在的问题是怎么只得到了最后一个?我看着这一点,它好像是3岁:

I have been searching and this problem seems simple but cannot find answer. I have multiple request calling different url. But for each url, I only want the result once and it must be the last one in the same url being called. My issue now is "how to get the last one only?" I looked at this and it seems to be 3 years old:

<一个href="http://plugins.jquery.com/project/ajaxqueue">http://plugins.jquery.com/project/ajaxqueue

任何其他方式很好,干净地做到这一点?如果有这样的事情,这将是完美的:

Any other way to do this nicely and cleanly? If there is something like this, it would be perfect:

queue: "getuserprofile",
cancelExisting: true

(凡在getuserprofile队列中的现有的AJAX将被取消)

(where the existing ajax in getuserprofile queue will be canceled)

感谢

推荐答案

<一个href="http://stackoverflow.com/questions/446594/kill-ajax-requests-using-javascript-using-jquery">This说明如何使用jQuery做一个Ajax调用,以及如何终止它。所有你需要做的就是创建一个存储每个请求的数组。然后,您可以同时加入了新的取消previous请求。

This explains how to use jQuery to do an ajax call and how to abort it. All you need to do is create an array that stores each request. You could then cancel the previous request while adding the new one.

ajaxRequests = new Array();

queueRequest = function(whatever, you, want) {
    if(ajaxRequests[ajaxRequests.length - 1]) {
        ajaxRequests[ajaxRequests.length - 1].abort();
    }

    ajaxRequests[ajaxRequests.length] = //Insert New jQuery AJAX call here.
}

这篇关于获取最新的AJAX请求,并放弃其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆