제이쿼리 callbacks.lock()

· 8년 전 · 1589
제이쿼리 callbacks.lock()

설명 : 현재 상태로 콜백 목록을 잠급니다.


"memory"플래그를 인수로 사용하여 콜백 객체를 만든 경우 콜백 목록이 잠긴 후에 추가 함수가 추가되고 실행될 수 있습니다.

예 :
사용 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


사용 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>
|
댓글을 작성하시려면 로그인이 필요합니다. 로그인

개발자팁

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
jQuery 8년 전 조회 1,483
jQuery
[jQuery]
8년 전 조회 1,950
PHP 8년 전 조회 6,941
jQuery 8년 전 조회 2,596
jQuery 8년 전 조회 1,632
jQuery 8년 전 조회 1,713
jQuery 8년 전 조회 1,855
jQuery 8년 전 조회 1,344
jQuery 8년 전 조회 1,900
jQuery 8년 전 조회 1,655
jQuery 8년 전 조회 1,941
jQuery 8년 전 조회 2,003
jQuery 8년 전 조회 1,623
jQuery 8년 전 조회 1,635
jQuery 8년 전 조회 1,590
jQuery 8년 전 조회 1,264
jQuery 8년 전 조회 1,427
jQuery 8년 전 조회 1,459
jQuery 8년 전 조회 1,373
jQuery 8년 전 조회 2,266
jQuery 8년 전 조회 1,749
PHP 8년 전 조회 3,781
jQuery 8년 전 조회 1,653
jQuery 8년 전 조회 1,930
jQuery 8년 전 조회 1,790
jQuery 8년 전 조회 1,540
jQuery 8년 전 조회 2,208
jQuery 8년 전 조회 1,736
jQuery 8년 전 조회 2,059
jQuery 8년 전 조회 1,851
🐛 버그신고