HomeLanguagesJavascriptJQuery .Deferred() method

JQuery .Deferred() method

This JQuery.Deferred() method in JQuery  is a function which returns the utility object with methods which can register multiple callbacks to queues.  It calls the callback queues, and relay the success or failure state of any synchronous or asynchronous function.

Syntax:

jQuery.Deferred([beforeStart])

    Parameters:

  • beforeStart: This is a function, which is called just before the constructor returns.

Return Value: This method creates and returns a new deferred object.

There are two examples discussed below:

  • Example: In this example, the Deferred() is used to create a new object and after that then() method is called with notify and resolve method.




    <!DOCTYPE HTML> 
    <html>  
    <head> 
        <title> 
          JQuery.Deferred() method
        </title>
    </script> 
    </head>   
    <body style="text-align:center;">
        <h1 style="color:green;">  
            GeeksForGeeks  
        </h1> 
        <p id="GFG_UP"> 
        </p>
        <button onclick = "Geeks();">
        click here
        </button>
        <p id="GFG_DOWN"> 
        </p>
        <script> 
            var el_up = document.getElementById("GFG_UP");
            el_up.innerHTML = "JQuery.Deferred() method";
            function Func1(val, div){
              $(div).append("From doneCallbacks - " + val);
            }
            function Func2(val, div){
              $(div).append("From failCallbacks - " + val);
            }
            function Func3(val, div){
              $(div).append("From progressCallbacks - " + val);
            }
            function Geeks() {
                var def = $.Deferred();
                def.then(Func1, Func2, Func3);
                def.notify(
    'Deferred "def" is notified.<br/>', '#GFG_DOWN');
                def.resolve(
    'Deferred "def" is resolved.<br/>', '#GFG_DOWN');
            } 
        </script> 
    </body>   
    </html>

    
    

    Output:

  • Example: In this example, the Deferred() method is used and the state of Deferred object is checked.




    <!DOCTYPE HTML> 
    <html>  
    <head> 
        <title> 
          JQuery.Deferred() method
        </title>
    </script> 
    </head>   
    <body style="text-align:center;">
        <h1 style="color:green;">  
            GeeksForGeeks  
        </h1> 
        <p id="GFG_UP"> 
        </p>
        <button onclick = "Geeks();">
        click here
        </button>
        <p id="GFG_DOWN"> 
        </p>
        <script> 
            var el_up = document.getElementById("GFG_UP");
            el_up.innerHTML = "JQuery.Deferred() method";
            var def = $.Deferred();
            def.resolve();
            function Geeks() {
                $('#GFG_DOWN').text(
           'deferred state is ' + def.state());
            } 
        </script> 
    </body>   
    </html>        

    
    
    • Output:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS