Top add

Jquery 1.5 deferred function

Jquery 1.5 has been released on 31st January. If includes existing bug fixing and ajax improvements. New methods Deferred is been introduced in 1.5

Deferred :

This function can be used in place for ajax. Suppose if we want to call a settimeout means we should manually call the javascript function. Now we can use this function

Example for settimeout:
$.wait = function(time) {
return $.Deferred(function(dfd) {
setTimeout(dfd.resolve, time);
});
}


$.wait(5000).then(function() {
alert("Hello Jquery 1.5");
});

the wait is the deferred function which can be used instead of settimeout. Like ajax the deferred has states like done, fail, rejected etc

Example for done, reject and fail deferred api 

function doSomething(arg) {
var dfr = $.Deferred();
setTimeout(function() {
dfr.reject("Sorry, something went wrong.");
});
return dfr;
}

doSomething("uh oh").done(function() {
alert("Won't happen, we're erroring here!");
}).fail(function(message) {
alert(message)
});

Advantages

  • Multiple call backs are present at once.
  • Error handling

Full Documentation at : JQUERY 1.5

No comments: