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 Atnaujinimai Via RSS | E-mail Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: Send function as… Pagrindinis / Kodai / JavaScript ir Ajax / JavaScript: Siųsti veikti kaip ...

JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Siųsti funkcija kaip parametro kitos funkcijos (Atgalinės iššūkius)

Posted on 29. Posted on 29. Jul, 2009 by Dragos in Coding , JavaScript & Ajax Liepa, 2009 Dragos ir kodavimo, JavaScript ir Ajax

I'm sure you've seen a lot of code where functions are send as parameters, usually working as function callbacks eg: Aš tikiu, kad jūs matėte kodo aikštelė, kur funkcijos yra siųsti kaip parametrus, dažniausiai dirba kaip funkcija Atgalinės iššūkius, pvz:

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

But how does the function setTimeout execute the passed function as a parameter? Tačiau kaip funkcija setTimeout vykdyti perduotas funkcijas parametras? The answer is simple, but I need to provide you an example to understand it Atsakymas yra paprastas, bet aš turiu pateikti jums pavyzdį, kad suprastų ;)

Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. Kurkime paprasta funkcija, priimdama du parametrus: pirma loginės vertės, antrosios - funkcija. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. Mūsų funkcija bus išanalizuoti boolean parametru ir tuo atveju, kai vertė yra teisinga, funkcijos perduodamas kaip parametras bus įvykdytas.

 function simpleFunc(bool,func) { funkcija simpleFunc (bool, func) ( 
     if(bool) func(); if (bool) func (); 
 } ) 

As you notice, we add parentheses after the name of the second parameter, because we'll treat it as a function. Kaip jūs pastebėjote, mes pridėti skliausteliuose po antrojo parametro pavadinimas, nes mes su ja elgiamasi, kaip funkcija. And that's te whole secret. Ir tai te visa paslaptis.

Now that is how we will use the simpleFunc function: Dabar tai, kaip mes naudosime simpleFunc Pareigos:

 simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false simpleFunc (false, function () (alert ( 'Yupee! ");) / / tai nebus pavojaus nieko, nes boolean parametras yra klaidinga 
 //example two / / pavyzdys dvi 
 simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (tiesa, function () (alert ( 'Yupee! ");)); / / bus įspėti Yupee! :)

Wish you luck! Wish you luck!

Translate this post Translate this post


Related posts: Related posts:

  1. Javascript: How to validate email address with JavaScript? Javascript: Kaip patvirtinti elektroninio pašto adresą su "JavaScript?
  2. JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Kaip gauti indeksą (pozicijos grupės) su jQuery objektas?
  3. JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Ką daryti, jei jQuery animacija nėra gaisro / paleisti?
  4. Coding: How to get code suggestions and function completion in Netbeans? Kodai: Kaip gauti kodą pasiūlymus ir funkcijos užbaigti Netbeans?
  5. Coding:How to fetch user profile data with SSI.php from a SMF forum database Kodai: Kaip įkelti naudotojo profilį duomenų SSI.php iš SMF forumas duomenų

    blog comments powered by Disqus Dienoraštis komentarus maitina Disqus