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 Oppdateringer via RSS | Epost Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: Send function as… Hjem / Coding / JavaScript og Ajax / JavaScript: Send fungere som ...

JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Send fungere som en parameter til en annen funksjon (tilbakeanrop)

Posted on 29. Skrevet den 29. Jul, 2009 by Dragos in Coding , JavaScript & Ajax Jul 2009 av Dragos i Coding, JavaScript og Ajax

I'm sure you've seen a lot of code where functions are send as parameters, usually working as function callbacks eg: Jeg er sikker på at du har sett mye kode der funksjoner blir sende som parametere, vanligvis jobber som funksjon tilbakeanrop f.eks:

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

But how does the function setTimeout execute the passed function as a parameter? Men hvordan funksjonen setTimeout kjøre forbi fungere som en parameter? The answer is simple, but I need to provide you an example to understand it Svaret er enkelt, men jeg må gi deg et eksempel for å forstå det ;)

Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. La oss lage en enkel funksjon, godtar to parametre: først en boolsk verdi, den andre - en funksjon. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. Våre funksjonen vil analysere boolske parameteren, og dersom verdien er sann, den funksjonen gikk som en parameter vil bli henrettet.

 function simpleFunc(bool,func) { funksjon 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. Som du ser, legger vi parentes etter navnet på den andre parameteren, fordi vi vil behandle det som en funksjon. And that's te whole secret. Og det er te hele hemmelig.

Now that is how we will use the simpleFunc function: Nå det er hvordan vi skal bruke simpleFunc funksjon:

 simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false simpleFunc (falske, function () (alert ( 'Yupee!');); / dette ikke vil varsle noe, fordi boolske parameteren er falsk 
 //example two / / eksempel to 
 simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (sann, function () (alert ( 'Yupee!');)); / / dette vil varsle Yupee! :)

Wish you luck! Ønsker deg lykke til!

Translate this post Oversett dette innlegget


Related posts: Relaterte artikler:

  1. Javascript: How to validate email address with JavaScript? Javascript: Hvordan kan validere e-postadressen med JavaScript?
  2. JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Hvordan få indeksen (posisjon innenfor en gruppe) av et objekt med jQuery?
  3. JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Hva hvis jQuery animasjon ikke brann / start?
  4. Coding: How to get code suggestions and function completion in Netbeans? Coding: Hvordan komme koden forslag og funksjon ferdigstillelse i NetBeans?
  5. Coding:How to fetch user profile data with SSI.php from a SMF forum database Coding: Hvordan hente brukerprofil data med SSI.php fra en SMF forum database

    blog comments powered by Disqus Bloggen kommentarer drevet av Disqus