天天看點

jQuery callbacks函數

callbacks函數方法:

callbacks.add() 将回調或回調的集合添加到回調清單。

callbacks.disable() 禁用回調清單做任何事情。

callbacks.disabled() 确定回調清單是否已禁用。

callbacks.empty() 從清單中删除所有回調。

callbacks.fire() 使用給定的參數調用所有回調。

callbacks.fired() 确定回調是否已經被調用至少一次。

callbacks.fireWith() 使用給定的上下文和參數調用清單中的所有回調。

callbacks.has() 确定清單是否附加了任何回調。 如果回調作為參數提供,則确定它是否在清單中。

callbacks.lock() 将回調清單鎖定在目前狀态。

callbacks.locked() 确定回調清單是否已被鎖定。

callbacks.remove() 從回調清單中删除回調或回調的集合。

callbacks.lock()示例:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>callbacks.lock demo</title>
  <script src="https://code.jquery.com/jquery-1.10.2.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>