ajax.js 999 B

1234567891011121314151617181920212223242526272829303132333435
  1. // Ajax mode: abort
  2. // usage: $.ajax({ mode: "abort"[, port: "uniqueport"]});
  3. // if mode:"abort" is used, the previous request on that port (port can be undefined) is aborted via XMLHttpRequest.abort()
  4. var pendingRequests = {},
  5. ajax;
  6. // Use a prefilter if available (1.5+)
  7. if ( $.ajaxPrefilter ) {
  8. $.ajaxPrefilter( function( settings, _, xhr ) {
  9. var port = settings.port;
  10. if ( settings.mode === "abort" ) {
  11. if ( pendingRequests[ port ] ) {
  12. pendingRequests[ port ].abort();
  13. }
  14. pendingRequests[ port ] = xhr;
  15. }
  16. } );
  17. } else {
  18. // Proxy ajax
  19. ajax = $.ajax;
  20. $.ajax = function( settings ) {
  21. var mode = ( "mode" in settings ? settings : $.ajaxSettings ).mode,
  22. port = ( "port" in settings ? settings : $.ajaxSettings ).port;
  23. if ( mode === "abort" ) {
  24. if ( pendingRequests[ port ] ) {
  25. pendingRequests[ port ].abort();
  26. }
  27. pendingRequests[ port ] = ajax.apply( this, arguments );
  28. return pendingRequests[ port ];
  29. }
  30. return ajax.apply( this, arguments );
  31. };
  32. }