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 Aggiornamenti via RSS | Email Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: Send function as… Home / Coding / JavaScript e Ajax / Javascript: Send funzione ...

JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Invia funzione come parametro a un'altra funzione (callback)

Posted on 29. Posted on 29. Jul, 2009 by Dragos in Coding , JavaScript & Ajax Luglio, 2009 da Dragos in Coding, JavaScript e Ajax

I'm sure you've seen a lot of code where functions are send as parameters, usually working as function callbacks eg: Sono sicuro che hai visto un sacco di codice in cui le funzioni vengono inviati come parametri, di solito a lavorare come funzione di callback per esempio:

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

But how does the function setTimeout execute the passed function as a parameter? Ma come fa il setTimeout funzione di eseguire la funzione passata come parametro? The answer is simple, but I need to provide you an example to understand it La risposta è semplice, ma ho bisogno di fornire un esempio per capire ;)

Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. Creiamo una semplice funzione, che accetta due parametri: in primo luogo un valore booleano, il secondo - una funzione. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. La nostra funzione di analizzare il parametro booleano e nel caso in cui il valore è true, la funzione passata come parametro verrà eseguito.

 function simpleFunc(bool,func) { funzione 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. Come si nota, si aggiunge tra parentesi dopo il nome del secondo parametro, perché ti trattano come una funzione. And that's te whole secret. E questo è tutto segreto te.

Now that is how we will use the simpleFunc function: Ora che è il modo in cui si utilizza la funzione simpleFunc:

 simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false simpleFunc (false, function () (alert ( 'Yupee!');) / / questo non sarà avviso nulla, perché il parametro booleano è false 
 //example two / / esempio due 
 simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (true, function () (alert ( 'Yupee!');)); / / questo avviso Yupee! :)

Wish you luck! Wish you luck!

Translate this post Traduci questo post


Related posts: Related posts:

  1. Javascript: How to validate email address with JavaScript? Javascript: Come validare indirizzo email con JavaScript?
  2. JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Come ottenere l'indice (posizione all'interno di un gruppo) di un oggetto con jQuery?
  3. JavaScript: What if jQuery animation doesn't fire/start? JavaScript: jQuery Che cosa succede se l'animazione non viene generato / start?
  4. Coding: How to get code suggestions and function completion in Netbeans? Coding: Come ottenere suggerimenti codice e completamento in funzione di Netbeans?
  5. Coding:How to fetch user profile data with SSI.php from a SMF forum database Coding: Come recuperare i dati del profilo utente con SSI.php da un database forum SMF

    blog comments powered by Disqus commenti del blog powered by Disqus