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>
输出: