How to chain ajax calls using jquery

With a custom object function DeferredAjax(opts) { this.options=opts; this.deferred=$.Deferred(); this.country=opts.country; } DeferredAjax.prototype.invoke=function() { var self=this, data={country:self.country}; console.log(“Making request for [” + self.country + “]”); return $.ajax({ type: “GET”, url: “wait.php”, data: data, dataType: “JSON”, success: function(){ console.log(“Successful request for [” + self.country + “]”); self.deferred.resolve(); } }); }; DeferredAjax.prototype.promise=function() { return this.deferred.promise(); }; var countries … Read more

Chain multiple “then” in jQuery.when

Use an anonymous function inside of .then and pass the parameters that you want to pass. I’m replacing .then with .done because you don’t need .then in this case. function do_something() { // some code return $.when(foo, bar, baz).done(function(_foo_2, _bar_2, _baz_2){ do_something_else.apply(this,_foo_2); }); } .then actually creates a new deferred object and sends that to … Read more

How do I use jQuery promise/deffered in a custom function?

You have to instantiate a new deferred object and return it (or its promise) from the function. Call its .resolve method once you get the response: var getLocation = function() { var deferred = new $.Deferred(); navigator.geolocation.getCurrentPosition(function( position ){ // Stuff with geolocation deferred.resolve(position); }); // return promise so that outside code cannot reject/resolve the … Read more

jQuery.when – Callback for when ALL Deferreds are no longer ‘unresolved’ (either resolved or rejected)?

I think the easiest way to do this is to keep a secondary Deferred object around for each AJAX request, and ensure that that one is always resolved: var d1 = $.Deferred(); var d2 = $.Deferred(); var j1 = $.getJSON(…).complete(d1.resolve); var j2 = $.getJSON(…).complete(d2.resolve); $.when(j1, j2).done(function() { // only fires if j1 AND j2 are … Read more

Retry a jquery ajax request which has callbacks attached to its deferred

You could use jQuery.ajaxPrefilter to wrap the jqXHR in another deferred object. I made an example on jsFiddle that shows it working, and tried to adapt some of your code to handle the 401 into this version: $.ajaxPrefilter(function(opts, originalOpts, jqXHR) { // you could pass this option in on a “retry” so that it doesn’t … Read more

Web Workers vs Promises

`Deferred/Promises and Web Workers address different needs: Deferred/promise are constructs to assign a reference to a result not yet available, and to organize code that runs once the result becomes available or a failure is returned. Web Workers perform actual work asynchronously (using operating system threads not processes – so they are relatively light weight). … Read more

Problems inherent to jQuery $.Deferred (jQuery 1.x/2.x)

Update: jQuery 3.0 has fixed the problems outlined below. It is truly Promises/A+ compliant. Yes, jQuery promises have serious and inherent problems. That said, since the article was written jQuery made significant efforts to be more Promises/Aplus complaint and they now have a .then method that chains. So even in jQuery returnsPromise().then(a).then(b) for promise returning … Read more

How to prevent the white ‘flash’ on page load created by background image loading delay?

Don’t delay loading parts of your site – what if the background image were to have an error in transmission and never arrive? Your scripts would never load. Instead, if you really dislike the “white” flash, set the background color of the document to a more pleasing color, more in line with your background image. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)