當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


jQuery deferred.reject()用法及代碼示例


jQuery中的deferred.reject()方法用於拒絕Deferred對象並使用給定參數調用所有failCallbacks。

用法:

deferred.reject( [args] )

參數:

  • args:它是傳遞給failCallbacks的可選參數。

返回值:此方法返回延遲的對象。

範例1:在此示例中,使用參數調用reject()方法。



html

<!DOCTYPE HTML> 
<html> 
  
<head> 
    <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> 
        jQuery | deferred.reject() method 
    </p> 
  
    <button onclick="Geeks();"> 
        click here 
    </button> 
      
    <p id="GFG"></p> 
  
    <script> 
        function Func(val, div) { 
            $(div).append(val); 
        } 
        function Geeks() { 
            var def = $.Deferred(); 
            def.fail(Func); 
            def.progress(Func); 
            def.reject('reject() method is ' 
                + 'called with arguments and' 
                + ' Deferred object is ' 
                + 'rejected', '#GFG') 
        }  
    </script> 
</body> 
  
</html>

輸出:

範例2:在此的示例reject()方法被調用而沒有參數。

html

<!DOCTYPE HTML> 
<html> 
  
<head> 
    <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> 
        JQuery | deferred.reject() method 
    </p> 
  
    <button onclick="Geeks();"> 
        click here 
    </button> 
  
    <p id="GFG"></p> 
  
    <script> 
        function Func() { 
            $('#GFG').append 
                ("reject() method is called " 
                + "without arguments and " 
                + "Deferred object is rejected"); 
        } 
          
        function Geeks() { 
            var def = $.Deferred(); 
            def.fail(Func); 
            def.progress(Func); 
            def.reject() 
        }  
    </script> 
</body> 
  
</html>

輸出:




相關用法


注:本文由純淨天空篩選整理自PranchalKatiyar大神的英文原創作品 jQuery deferred.reject() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。