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


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


JQuery中的deferred.notifyWith()方法用於調用Deferred對象上的progressCallbacks以及提供的上下文和args。句法:

deferred.notifyWith(context[, args])

參數:

  • context:此參數是作為‘this’對象傳遞給progressCallbacks的上下文。
  • args:此參數是傳遞給progressCallbacks的可選參數數組。

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

下麵討論了兩個示例:
示例1:在此示例中,我們用兩個參數通知Deferred對象,並在拒絕它之前處理所有progressCallbacks。

<!DOCTYPE HTML>  
<html>   
<head>  
    <title>  
      JQuery | deferred.notifyWith() 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.notifyWith() method"; 
        function Func(val, div){ 
          $(div).append('From function "Func":' + val); 
        } 
        function Geeks() { 
            var def = $.Deferred(); 
            def.progress(Func); 
            def.notifyWith( 
this, ['notifyWith() is called with arguments. <br />', '#GFG_DOWN']); 
        }  
    </script>  
</body>    
</html>        
     

輸出:

示例2:在此示例中,我們僅使用一個參數通知Deferred對象,並在解決它之前處理所有progressCallbacks。

<!DOCTYPE HTML>  
<html>   
<head>  
    <title>  
      JQuery | deferred.notifyWith() 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.notifyWith() method"; 
        function Func(val, div){ 
          $(div).append('From function "Func":' + val); 
        } 
        function Geeks() { 
            var def = $.Deferred(); 
            def.done(Func); 
            def.progress(Func); 
            def.notifyWith(this, ['#GFG_DOWN']); 
            def.resolve('Deferred is resolved.<br />', '#GFG_DOWN') 
        }  
    </script>  
</body>    
</html>  

輸出:




相關用法


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