This is a translated page. The original can be found here: http://iwebdevel.com/2009/07/29/javascript-send-function-as-a-parameter-to-another-function-callbacks/
UPDATES VIA RSS | Email 업데이 트를 통해 RSS | 이메일 Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: Send function as… / 코딩 / 자바 스크립트 & 아약스 / 자바 스크립트 : 함수를 보내기로 ...

JavaScript: Send function as a parameter to another function (callbacks) 자바 스크립트 : 다른 함수 (콜백)를 매개 변수로 함수를 보내기

Posted on 29. 29 게시됨. Jul, 2009 by Dragos in Coding , JavaScript & Ajax 7월, 2009 코딩, 자바 스크립트 & 아약스에서 드라 고스에 의해

I'm sure you've seen a lot of code where functions are send as parameters, usually working as function callbacks eg: 난 당신의 코드를 어디에 함수를 매개 변수로 보낼 수있습니다 많은 예를 들어, 일반적으로 함수를 콜백으로 일하고 본적 확신 :

setTimeout(function () { alert('test'); },1000); setTimeout (함수 () () ( '검사'경고;), 1000);

But how does the function setTimeout execute the passed function as a parameter? 그런데 어떻게 setTimeout 함수를 매개 변수로 전달 기능을 수행합니까? The answer is simple, but I need to provide you an example to understand it 대답은 간단합니다,하지만 당신은 그것을 이해하고 예제를 제공해야합니다 ;)

Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. 이 간단한 함수를 만드는 두 가지 매개 변수를 수락하자 : boolean 값을 첫 번째, 두 번째 - 기능. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. 우리의 경우 기능과 가치, 함수 매개 변수로 전달 실행될 것입니다 사실이다 부울 매개 변수를 분석하는 것입니다.

function simpleFunc(bool,func) { 함수 simpleFunc (func) (bool
    if(bool) func(); 만약 (bool) func ();
} )

As you notice, we add parentheses after the name of the second parameter, because we'll treat it as a function. 당신이 통지를하기 때문에 우리는 함수로 취급거야, 우리는 두 번째 매개 변수의 이름을 따서, 괄호를 추가합니다. And that's te whole secret. 그리고 저 소리지르 다 비밀이야.

Now that is how we will use the simpleFunc function: 이제 우리가 어떻게 simpleFunc 함수를 사용하는 것입니다 :

simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false simpleFunc (거짓, 함수 () (( 'Yupee 경보!');); 때문에 부울 매개 변수를 false입니다 / / 이것은, 아무것도 경고하지 않습니다
//example two / / 예제 2
simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (사실, 함수 () (( 'Yupee 경보!');)); / /이 Yupee 알려드립니다! :)

Wish you luck! 행운을 빌어!

Translate this post 번역이 게시물에


Related posts: 관련 게시물 :

  1. Javascript: How to validate email address with JavaScript? 자바 스크립트 : 자바 스크립트와 방법의 이메일 주소를 확인?
  2. JavaScript: How to get the index (position within a group) of an object with jQuery? 자바 스크립트 : 어떻게 jQuery와 개체의 (위치를 그룹 내에서) 인덱스를 얻을가?
  3. JavaScript: What if jQuery animation doesn't fire/start? 자바 스크립트 : 어떤 경우에는 jQuery 애니메이션 쏘지 / 시작합니까?
  4. Coding: How to get code suggestions and function completion in Netbeans? 코딩 : 코드를 제안하는 방법 및 넷빈즈 함수를 완성 해볼까?
  5. Coding:How to fetch user profile data with SSI.php from a SMF forum database 코딩 : 어떻게 SMF 포럼 SSI.php와 데이터베이스에서 사용자 프로필 데이터를 가져올 수

    blog comments powered by Disqus 블로그 덧글 Disqus에 의해 제공