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 Päivitykset RSS | Sähköposti Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: Send function as… Etusivu / koodaus / JavaScript ja Ajax / JavaScript: Lähetä toimia ...

JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Lähetä toimivat parametri toisen toiminnon (kutsuja)

Posted on 29. Postitettu 29. Jul, 2009 by Dragos in Coding , JavaScript & Ajax Heinäkuu, 2009 Dragos on Coding, JavaScript-ja Ajax

I'm sure you've seen a lot of code where functions are send as parameters, usually working as function callbacks eg: Olen varma, että olet nähnyt paljon koodia, jos toimintoja lähettää parametreiksi, yleensä työskentelee toiminto kutsuja esimerkiksi:

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

But how does the function setTimeout execute the passed function as a parameter? Mutta miten toimia setTimeout suorittaa kulunut toimii parametri? The answer is simple, but I need to provide you an example to understand it Vastaus on yksinkertainen, mutta minun on annettava teille esimerkin ymmärtää sitä ;)

Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. Let's luoda yksinkertainen tehtävä, hyväksyi kaksi muuttujaa: ensimmäinen boolean arvo, toinen - toiminto. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. Meidän toiminto analysoi boolean parametri, ja jos arvo on tosi, funktio välitetään parametrina toteutetaan.

 function simpleFunc(bool,func) { toiminto simpleFunc (bool, func) ( 
     if(bool) func(); jos (bool) func (); 
 } ) 

As you notice, we add parentheses after the name of the second parameter, because we'll treat it as a function. Kuten huomaat, lisäämme jälkeen suluissa nimen toinen parametri, koska me käsittelemme sitä funktiona. And that's te whole secret. Ja se on te koko salaisuus.

Now that is how we will use the simpleFunc function: Nyt kun on, miten käytämme simpleFunc toimintoa:

 simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false simpleFunc (false, function () (alert ( 'Yupee! ");), / / tämä ei hälytyksen mitään, koska boolean parametri on virheellinen 
 //example two / / esimerkiksi kaksi 
 simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (true, function () (alert ( 'Yupee! ");)), / / tämä hälyttää Yupee! :)

Wish you luck! Toivotan teille paljon onnea!

Translate this post Käännä tämä viesti


Related posts: Liittyvien virkojen:

  1. Javascript: How to validate email address with JavaScript? Javascript: Miten vahvistaa sähköpostiosoitteesi JavaScript?
  2. JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Miten saada indeksi (asema ryhmä) objektin kanssa jQuery?
  3. JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Mitä jos jQuery animaatio ei palo / alku?
  4. Coding: How to get code suggestions and function completion in Netbeans? Koodaus: Miten saada koodin ehdotuksia ja toiminnan päätyttyä vuonna Netbeans?
  5. Coding:How to fetch user profile data with SSI.php from a SMF forum database Koodaus: Miten hakea käyttäjäprofiilin tietojen SSI.php peräisin SMF foorumin tietokanta

    blog comments powered by Disqus blogin kommentit powered by Disqus