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 GÜNCELLEME VIA RSS | E-posta Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: Send function as… Ana Sayfa / Kodlama / JavaScript & Ajax / JavaScript: olarak işlev gönder ...

JavaScript: Send function as a parameter to another function (callbacks) JavaScript: başka fonksiyonu (geriçağırımları) için parametre olarak fonksiyonu Email

Posted on 29. 29 olarak gönderildi. Jul, 2009 by Dragos in Coding , JavaScript & Ajax Temmuz, 2009 Kodlama, JavaScript ve Ajax da Dragos tarafından

I'm sure you've seen a lot of code where functions are send as parameters, usually working as function callbacks eg: Sana kod gibi fonksiyon parametre olarak gönderirsiniz çok, genellikle fonksiyon geriçağırımları örneğin olarak görev gördüm eminim:

 setTimeout(function () { alert('test'); },1000); setTimeout (function () () ( 'test' uyarı;), 1000); 

But how does the function setTimeout execute the passed function as a parameter? Ama nasıl setTimeout fonksiyonu parametre olarak geçti işlevi çalıştırıyor? The answer is simple, but I need to provide you an example to understand it Cevap basit, ama bunu anlamak için bir örnek vermek gerekir ;)

Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. Basit bir fonksiyon oluşturmak, iki parametre kabul edelim: Önce bir boolean değeri, ikinci - bir işlev. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. Bizim fonksiyon ve durumda değeri, fonksiyonu parametre olarak kabul yürütülür doğrudur boolean parametre analiz eder.

 function simpleFunc(bool,func) { fonksiyonu simpleFunc (func) (bool 
     if(bool) func(); if (bool) işlev (); 
 } ) 

As you notice, we add parentheses after the name of the second parameter, because we'll treat it as a function. Siz fark çünkü bir fonksiyon gibi davranın olacak, biz ikinci parametre adından sonra, parantez ekleyin. And that's te whole secret. And that's te sırrı.

Now that is how we will use the simpleFunc function: Şimdi nasıl simpleFunc işlevini kullanın edecektir:

 simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false simpleFunc (yanlış, function () (( 'Yupee alert');); çünkü boolean parametre yanlıştır / / Bu, hiçbir uyarı olmaz 
 //example two / / Örneğin iki 
 simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (gerçek, function () (( 'Yupee alert');)) / / Bu Yupee uyarır! :)

Wish you luck! Wish you luck!

Translate this post Translate bu yazı


Related posts: Ilgili posta:

  1. Javascript: How to validate email address with JavaScript? Javascript: nasıl JavaScript ile email adresinizi doğrulamak için?
  2. JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: jQuery ile nasıl bir nesnenin (konum bir grup içinde) kütüğü almak için?
  3. JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Ya jQuery animasyon değil yangın / başlar?
  4. Coding: How to get code suggestions and function completion in Netbeans? Kodlama: Nasıl kod önerileri ve Netbeans işlev tamamlanması için?
  5. Coding:How to fetch user profile data with SSI.php from a SMF forum database Kodlama: Nasıl Bir SMF forumunun veritabanından SSI.php ile kullanıcı profili veri almak için

    blog comments powered by Disqus Blog Yorumlar Disqus tarafından desteklenmektedir