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 Ažuriranja putem RSS | E-mail Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: Send function as… Home / Kodiranje / Ajax & JavaScript / JavaScript: Pošalji funkcionirati kao ...

JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Pošaljite funkcija kao parametar za druge funkcije (callbacks)

Posted on 29. Objavljeno 29.. Jul, 2009 by Dragos in Coding , JavaScript & Ajax Srp, 2009 by Dragos u kodiranju, Ajax & JavaScript

I'm sure you've seen a lot of code where functions are send as parameters, usually working as function callbacks eg: Siguran sam da ste vidjeli puno koda, gdje se poslati kao funkcije parametara, obično radi kao funkcija callbacks npr.:

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

But how does the function setTimeout execute the passed function as a parameter? No, kako se funkcije setTimeout izvršiti prošao funkcija kao parametar? The answer is simple, but I need to provide you an example to understand it Odgovor je jednostavan, ali moram dati vam primjer da ga razumijem ;)

Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. Let's stvoriti jednostavan funkcije, prihvaćajući dva parametra: prvi boolean vrijednost, drugi - funkcija. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. Naša funkcija će analizirati boolean parametar iu slučaju vrijednost je istina, funkcija prosljeđena kao parametar će biti pogubljen.

 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. Kao što ste obavijest, možemo dodati zagrade nakon naziva drugi parametar, jer ćemo ga tretirati kao funkcije. And that's te whole secret. A to je te cijela tajna.

Now that is how we will use the simpleFunc function: Sada kada je kako ćemo iskoristiti simpleFunc funkcije:

 simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false simpleFunc (lažni, function () (alert ( 'Yupee!');); / / to upozorenje neće ništa, jer boolean parametar je lažna 
 //example two / / primjer dva 
 simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (istina, function () (alert ( 'Yupee!');)); / / to će upozorenje Yupee! :)

Wish you luck! Želi vam sreću!

Translate this post Translate this post


Related posts: Related posts:

  1. Javascript: How to validate email address with JavaScript? Javascript: Kako da biste provjerili valjanost adresa e-pošte s JavaScriptom?
  2. JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Kako biste dobili indeks (poziciju unutar grupe) s jQuery objekt?
  3. JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Što ako jQuery animacija ne požara / početi?
  4. Coding: How to get code suggestions and function completion in Netbeans? Coding: Kako dobiti kod prijedloge i funkcija završena u NetBeans?
  5. Coding:How to fetch user profile data with SSI.php from a SMF forum database Coding: Kako radi pristizanja podataka korisnik s SSI.php profil iz baze podataka SMF forum

    blog comments powered by Disqus Blog komentari powered by Disqus