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


jQuery callbacks.lock()用法及代码示例


jQuery中的callbacks.lock()方法用于将回调列表锁定为状态。

用法:

callbacks.lock()

返回值:此方法返回其附加到的回调对象。

范例1:此示例首先添加并触发该函数,然后锁定回调列表,然后再次添加并触发该函数。

<!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 | callbacks.lock() method 
    </p> 
      
    <button onclick="Geeks();"> 
        click here 
    </button> 
      
    <p id="GFG_DOWN"></p> 
  
    <script> 
        var el_down = document 
                .getElementById("GFG_DOWN"); 
                  
        var res = ""; 
        var callbacks = jQuery.Callbacks(); 
        function Geeks() { 
            var func = function (val) { 
                res = res +  
                    "value passed is - " + val; 
            }; 
  
            // Function added to list 
            callbacks.add(func); 
            callbacks.fire("gfg_1"); 
  
            // Locking the callback list 
            callbacks.lock();  
  
            // Function again added to list 
            callbacks.add(func);  
            callbacks.fire("gfg_2"); 
            el_down.innerHTML = res; 
        }  
    </script> 
</body> 
  
</html>

输出:

范例2:本示例提供了一个按钮,用于锁定列表,然后添加并触发该函数以检查该方法是否有效。

<!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 | callbacks.lock() method 
    </p> 
  
    <button onclick="Geeks();"> 
        click here 
    </button> 
      
    <button onclick="lock();"> 
        lock here 
    </button> 
      
    <p id="GFG_DOWN"></p> 
  
    <script> 
        var el_down = document 
                .getElementById("GFG_DOWN"); 
  
        var res = ""; 
        var callbacks = jQuery.Callbacks(); 
        function lock() { 
            callbacks.lock(); 
        } 
        function Geeks() { 
  
            // Function to be added to the list 
            var fun = function (val) { 
                res = res + "This is function and " 
                + "value passed is " + val + "<br>"; 
            }; 
  
            callbacks.add(fun); // Adding 
            callbacks.fire("GFG_1"); 
            el_down.innerHTML = res; 
        }  
    </script> 
</body> 
  
</html>

输出:




相关用法


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