当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


jQuery deferred.state()用法及代码示例


JQuery中的deferred.state()方法用于确定Deferred对象的当前状态。句法:

deferred.state()

返回值:此方法返回延迟对象的状态。

下面讨论了两个示例:

  • 例:在此示例中,延迟对象‘def’的状态为未决。
    <!DOCTYPE HTML>  
    <html>   
    <head>  
        <title>  
          JQuery | deferred.state() method 
        </title> 
        <script src="https://code.jquery.com/jquery-3.5.0.js"> 
    </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.state() method"; 
            var def = $.Deferred(); 
            function Geeks() { 
             $('#GFG_DOWN').text('deferred state is ' + def.state()); 
            }  
        </script>  
    </body>    
    </html>
  • 输出:
  • 例:在此示例中,延迟对象‘def’的状态为挂起状态,并且通过单击按钮将其更改为“已解决”。
    <!DOCTYPE HTML>  
    <html>   
    <head>  
        <title>  
          JQuery | deferred.state() method 
        </title> 
        <script src="https://code.jquery.com/jquery-3.5.0.js"> 
    </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.state() method"; 
            var def = $.Deferred(); 
            def.always(Func); 
            $('#GFG_DOWN').append( 
              'Deferred state is ' + def.state() + '</br>'); 
            function Func(val, div){ 
              $(div).append(val); 
            } 
            function Geeks() { 
                def.resolve( 
          'Our deferred is resolved now. <br />', '#GFG_DOWN'); 
                $('#GFG_DOWN').append(  
    'Current state of deferred is ' + def.state() + ' <br />'); 
            }  
        </script>  
    </body>    
    </html>
  • 输出:




相关用法


注:本文由纯净天空筛选整理自PranchalKatiyar大神的英文原创作品 JQuery deferred.state() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。