Home>

Hi. Interested in where you can use this object? The Deferred object is used in AJAX handlers. As I understood it, you can use to create your AJAX handlers to track the result of the sequence of AJAX queries:

var newdef= $ .when ($ GET ("test1.php"), $ .get ("test2.php"));
//Set the handlers of the NEWDEF
newdef.done (Function () {
  Alert ("2 AJAX requests successfully completed");
}). Fail (FUNCTION () {
  Alert ("When performing one of the AJAX queries, an error occurred");
});

Where else can this object be useful? When working with conventional functions (not AJAX), it is apparently not used?

  • Answer # 1

    Well, why, the same $ ('...'). Animate () -Returns $ .Deferred, you can even make it like this:

    http://jsfiddle.net/rubaxa/uxjhf/

    $. When (
       $ .get ('/user /123'),
       $ .get ('/settings /'),
       $ ('# Bar'). Animate ({width: '100%'})
    ) .Then (Function () {
        //Function will be executed after completing AJAH
        //Query and Animations
    });
    //or
    FUNCTION MYCONFIRM (MSG) {
        VAR DFD= $ .Deferred ();
        VAR $ EL= $ ('<
    Div >
    '+.
                    '≪
    Div >
    '+ MSG +' <
    /div >
    '+.
                    '≪
    Button class= "Yes" >
    YES <
    /Button >
     or '+.
                    '≪
    Button class= "no" >
    NO <
    /Button >
    '+.
                    '≪
    /div >
    ')
            .APPendTo ('Body')
            .on ('Click', '.yes', DFD.Resolve)
            .on ('click', '.no', dfd.reject)
        ;
        DFD.ALWAYS (FUNCTION () {
            $ el.hide ('slow');
        });
        Return dfd.promise ();
    }
    MyConfirm ("Deffered")
      .done (function () {console.log ("yes");})
      .fail (Function () {Console.log ("No");})
    ;
    

    p.s. Well, on Habré, full of articles on this topic.