Isn't it just a matter of storing the $.ajax object for your request/s
then using abort() on any that you want to cancel?
eg. (in a simplified example)
var ajax = {
xhr: null,
send: function() {
if (ajax.xhr) ajax.xhr.abort();
ajax.xhr = $.ajax({...});
}
};
ajax.send();
Extending on that - if you want to manage and cancel a 'collection /
group / all' running xhr requests, then instead of storing the $.ajax
in a single variable, you'd store it in an array or object (eg. xhr
['...'] = $.ajax() etc), and then loop over them to cancel each.
I have used this approach on two web apps that are very similar in
requirements / user interaction to what you've described.
*** NOTE ***
The above code will cause uncleared timeouts and increased CPU usage
with every aborted request in the current version of jQuery because of
a bug in jQuery that I lodged a ticket for 6 months ago:
http://dev.jquery.com/ticket/2935
Jörn submitted a patch 5 months ago, but there hasn't been an update
to jQuery, so I'm hoping that it'll be added to the next release.
In the meantime, if you implement the patch you'll be able to use abort
() properly until it gets fixed in the core.
While we're on the subject, is it possible to get an update on the
status of that ticket?
Ian
On Nov 13, 5:48 am, Ego-Ale-Sum <[EMAIL PROTECTED]> wrote:
> Uhm....
> I'm not sure I do understand the code: it's not documented at all.
> Anyways, seems like it's a queue system: requests are not in parallel,
> but executed once at time. So, it's different from my code!
>
> On 12 Nov, 20:35, "Jörn Zaefferer" <[EMAIL PROTECTED]>
> wrote:
>
> > The release there is quite different from the version in svn, which
> > may be closer to what you are looking
> > for:http://dev.jquery.com/browser/trunk/plugins/ajaxQueue/jquery.ajaxQueu...
> > The docs are all wrong, but the usage is really simple:
>
> > No ports:
>
> > $.ajax({
> > mode: "abort"
>
> > });
>
> > With ports:
>
> > $.ajax({
> > mode: "abort",
> > port: "autocomplete"
>
> > });
>
> > Ports are useful when you don't want to cancel everything, but only a
> > certain type of request, like those for a autocomplete component.
>
> > Jörn
>
> > On Wed, Nov 12, 2008 at 8:20 PM, John Resig <[EMAIL PROTECTED]> wrote:
>
> > > Really quickly - I was wondering if this Ajax Queue plugin helped you
> > > at all (or if you saw it):
> > >http://plugins.jquery.com/project/ajaxqueue
>
> > > We worked on that one last year - just wondering if it helps you at
> > > all, before we look at other solutions.
>
> > > --John
>
> > > On Wed, Nov 12, 2008 at 11:49 AM, Ego-Ale-Sum <[EMAIL PROTECTED]> wrote:
>
> > >> In my website, I'm using jQuery to handle XHR requests (using the
> > >> methods $.post, $.get, $.getScript and even the AJAX form plugin).
> > >> I wanted XHR requests to stop if new ones are made.
> > >> I've found a plugin named "AJAX Manager" (or something similar) for
> > >> that, but it was not what I was looking for: first, it supports just
> > >> $.get requests and it keeps all the active requests in a sort of
> > >> queue.
>
> > >> I needed something that stopped all the previous XHR requests when a
> > >> new one is made.
> > >> I thought the best way to accomplish that was simply not to trigger
> > >> the callback after the request completes.
>
> > >> I looked inside the jQuery library and I found that the problem could
> > >> not be solved with a plugin (I needed to edit the $.ajax method
> > >> directly).
> > >> So, I've realized a simple but powerful patch.
>
> > >> I'm posting the patch here: I hope it will be useful.
> > >> Also, since I think it's a quite useful addition, I'm asking the
> > >> developers wheter they could add it to the trunk.
>
> > >> Here's the patch (from my SVN repository):
>
> > >> Index: jquery.js
> > >> ===================================================================
> > >> --- jquery.js (revision 188)
> > >> +++ jquery.js (revision 190)
> > >> @@ -2512,6 +2512,9 @@
> > >> var jsc = now();
>
> > >> jQuery.extend({
> > >> + // The current request
> > >> + ajaxRequestId: 0,
> > >> +
> > >> get: function( url, data, callback, type ) {
> > >> // shift arguments if data argument was ommited
> > >> if ( jQuery.isFunction( data ) ) {
> > >> @@ -2564,6 +2567,8 @@
> > >> processData: true,
> > >> async: true,
> > >> data: null,
> > >> + newRequestAbort: false,
> > >> + requestId: 0,
> > >> username: null,
> > >> password: null,
> > >> accepts: {
> > >> @@ -2586,6 +2591,10 @@
>
> > >> var jsonp, jsre = /=\?(&|$)/g, status, data,
> > >> type = s.type.toUpperCase();
> > >> +
> > >> + // increment the request counter
> > >> + jQuery.ajaxRequestId++;
> > >> + s.requestId = jQuery.ajaxRequestId;
>
> > >> // convert data if not already a string
> > >> if ( s.data && s.processData && typeof s.data != "string"
> > >> )
> > >> @@ -2813,9 +2822,12 @@
> > >> onreadystatechange();
>
> > >> function success(){
> > >> - // If a local callback was specified, fire it
> > >> and pass it the data
> > >> - if ( s.success )
> > >> - s.success( data, status );
> > >> + if(!s.newRequestAbort || s.requestId ==
> > >> jQuery.ajaxRequestId)
> > >> + {
> > >> + // If a local callback was specified,
> > >> fire it and pass it the
> > >> data
> > >> + if ( s.success )
> > >> + s.success( data, status );
> > >> + }
>
> > >> // Fire the global callback
> > >> if ( s.global )
> > >> @@ -2842,7 +2854,7 @@
>
> > >> handleError: function( s, xhr, status, e ) {
> > >> // If a local callback was specified, fire it
> > >> - if ( s.error ) s.error( xhr, status, e );
> > >> + if ( s.error && (!s.newRequestAbort || s.requestId ==
> > >> jQuery.ajaxRequestId) ) s.error( xhr, status, e );
>
> > >> // Fire the global callback
> > >> if ( s.global )
>
> > >> Here's a test file
>
> > >> <?php
> > >> if(!empty($_GET['ajax']))
> > >> {
> > >> sleep(3);
> > >> echo "Received ".$_GET['ajax'];
> > >> exit;
> > >> }
> > >> ?>
> > >> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
> > >>www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
> > >> <html xmlns="http://www.w3.org/1999/xhtml">
> > >> <head>
> > >> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
> > >> <title>Untitled Document</title>
> > >> <script type="text/javascript" src="app/clientscript/jquery.js"></
> > >> script>
> > >> <script type="text/javascript">
> > >> //<!--
> > >> var id = 0;
> > >> function theCallback(data)
> > >> {
> > >> $("#result").append(data+"<br/>");
> > >> }
>
> > >> function doRequest(abort)
> > >> {
> > >> id++;
> > >> if(!abort)
> > >> {
> > >> $.ajaxSetup({newRequestAbort: false});
> > >> }
> > >> else
> > >> {
> > >> $.ajaxSetup({newRequestAbort: true});
> > >> }
> > >> $.get("test.php", {ajax: id}, theCallback);
> > >> }
> > >> //-->
> > >> </script>
> > >> </head>
> > >> <body>
> > >> <a href="javascript:doRequest(true)">do request that aborts on new
> > >> request</a><br />
> > >> <a href="javascript:doRequest(false)">do request that does NOT abort
> > >> on new requests</a>
> > >> <div id="result"></div>
> > >> </body>
> > >> </html>
>
> > >> I hope my work can help many other users that every day choose this
> > >> fantastic javascript framework.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"jQuery Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---