Original link: http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html
Normal operation callback function Method 1
var wait = function (DTD) {var DTD = $. Deferred (); Inside the function, create a new deferred object var tasks = function () {alert ("Execution complete! "); Dtd.resolve (); Change the execution state of the deferred object}; SetTimeout (tasks,5000); return Dtd.promise (); Returns the Promise object}; $.when (Wait ()). Done (function () {alert ("Haha, success! "); }) . Fail (function () {alert ("Error! "); });
Normal operation callback function method 2
var wait = function (DTD) {var tasks = function () {alert ("Execution complete! "); Dtd.resolve (); Change the execution state of the deferred object}; SetTimeout (tasks,5000); return Dtd.promise (); }; $. Deferred (Wait). Done (function () {alert ("Haha, success! "); }) . Fail (function () {alert ("Error! "); });
Normal Operation callback function Method 3
var DTD = $. Deferred (); Generate deferred object var wait = function (DTD) {var tasks = function () {alert ("Execution complete! "); Dtd.resolve (); Change the execution state of the deferred object}; SetTimeout (tasks,5000); }; Dtd.promise (wait); Wait.done (function () {alert ("Haha, success!") "); }) . Fail (function () {alert ("Error! "); }); Wait (DTD);
jquery Asynchronous deferred