Delay JQuery Ajax (jqXHR) Request with done/fail/complete callbacks -
when use success callback solution works fine, when use .done() fail, how can retry send enqueued ajax request original .done() .fail() , complete() registered callbacks?
var requestqueue = []; $.ajaxsetup({ cache: false, beforesend: function (jqxhr, options) { if(true){ //any condition 'true' demonstrate requestqueue.push({request:jqxhr,options:options}); //simulate process queue later resend request window.settimeout(function(){ //this work success callbak option, //but .done() console.log("well done!"); // fail $.ajax($.extend(requestqueue.pop().options, {global:false, beforesend:null})); }, 3000) return false; } } }); $.ajax({ url:"testechanged.html", error: function(){ console.log("oh nooooo!"); } }).done(function(){ console.log("well done!"); });
i wanna enqueue ajax call (based in condition) resend later, when resend it, .done()/.fail() original callback must called. 'success' callback option code works fine.
i use delaying ajax requests:
global variant:
var origsend = xmlhttprequest.prototype.send; xmlhttprequest.prototype.send = function () { var xhr = this; var origarguments = arguments; settimeout(function () { if (xhr.readystate === 1) { origsend.apply(xhr, origarguments); } }, 1000); };
vairant affects jquery ajax requests:
$(document).ajaxsend(function (event, jqxhr, settings) { var origxhrfunc = settings.xhr; settings.xhr = function () { var xhr = origxhrfunc(); var origsend = xhr.send; xhr.send = function () { var origarguments = arguments; settimeout(function () { if (xhr.readystate === 1) { origsend.apply(xhr, origarguments); } }, 1000); }; return xhr; }; });
in jquery solution, can attach handlers jqxhr done/fail/progress events.
Comments
Post a Comment