您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關callbacks.lock()方法怎么用,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
callbacks.lock()
描述: 鎖定回調列表的當前狀態。
添加的版本: 1.7callbacks.lock()
這個方法不接受任何參數
此方法返回綁定它的那個回調對象(this).
如果回調對象被創建,用"memory"標志作為它的參數,綁定函數可能會在回調列表中被鎖定后增加并且觸發。
例子:
Example: 用 callbacks.lock() 鎖定一個回調列表,以避免進一步的修改列表狀態 :
// a sample logging function to be added to a callbacks list
var foo = function( value ) {
console.log( "foo:" + value );
};
var callbacks = $.Callbacks();
// add the logging function to the callback list
callbacks.add( foo );
// fire the items on the list, passing an argument
callbacks.fire( "hello" );
// outputs "foo: hello"
// lock the callbacks list
callbacks.lock();
// try firing the items again
callbacks.fire( "world" );
// as the list was locked, no items
// were called, so "world" isn"t logged
Example: Use callbacks.lock() to lock a callback list with "memory," and then resume using the list:
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<div id="log"></div>
<script>// simple function for logging results
var log = function( value) {
$( "#log" ).append( "<p>" + value + "</p>" );
};
// two sample functions to be added to a callbacks list
var foo = function( value ) {
log( "foo: " + value );
};
var bar = function( value ) {
log( "bar: " + value );
};
// create the callbacks object with the "memory" flag
var callbacks = $.Callbacks( "memory" );
// add the foo logging function to the callback list
callbacks.add( foo );
// fire the items on the list, passing an argument
callbacks.fire( "hello" );
// outputs "foo: hello"
// lock the callbacks list
callbacks.lock();
// try firing the items again
callbacks.fire( "world" );
// as the list was locked, no items were called,
// so "foo: world" isn't logged
// add the foo function to the callback list again
callbacks.add( foo );
// try firing the items again
callbacks.fire( "silentArgument" );
// outputs "foo: hello" because the argument value was stored in memory
// add the bar function to the callback list
callbacks.add( bar );
callbacks.fire( "youHadMeAtHello" );
// outputs "bar: hello" because the list is still locked,
// and the argument value is still stored in memory
</script>
</body>
</html>
關于“callbacks.lock()方法怎么用”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。