JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Send fungere som en parameter til en anden funktion (tilbagekald)
Posted on 29. Sendt den 29. Jul, 2009 by Dragos in Coding , JavaScript & Ajax Juli, 2009 af Dragos i Kodning, 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å, du har set en masse kode, hvor funktioner sende som parametre, der normalt arbejder som funktion tilbagekald, fx:
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 fungerer setTimeout udføre bestået fungere som en parameter? The answer is simple, but I need to provide you an example to understand it Svaret er enkelt, men jeg har brug for at give dig et eksempel for at forstå det ![]()
Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. Lad os oprette en simpel funktion, accepterer to parametre: For det første en boolesk værdi, den anden - en funktion. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. Vores funktion vil analysere den boolske parameter, og i tilfælde af værdien er sandt, funktionen gik som en parameter vil blive henrettet.
function simpleFunc(bool,func) { function 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 bemærker, vi tilføjer parentes efter navnet på den anden parameter, fordi vi vil behandle den som en funktion. And that's te whole secret. Og det er te hele hemmelighed.
Now that is how we will use the simpleFunc function: Nu, er, hvordan vi vil bruge den simpleFunc funktion:
simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false simpleFunc (falsk, function () (alert ( 'Yupee!');) / / dette vil ikke advare noget, fordi den boolske parameter er falsk //example two / / eksempel to simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (true, function () (alert ( 'Yupee!');)); / / dette vil advare Yupee!![]()
Wish you luck! Ønsker jer held og lykke!
Related posts: Relaterede stillinger:
- Javascript: How to validate email address with JavaScript? Javascript: Hvordan at validere e-mail-adresse med JavaScript?
- JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Sådan får du indekset (position inden for en gruppe) af et objekt med jQuery?
- JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Hvad hvis jQuery animationen ikke brand / start?
- Coding: How to get code suggestions and function completion in Netbeans? Coding: Sådan får du koden forslag og funktion afsluttet i Netbeans?
- Coding:How to fetch user profile data with SSI.php from a SMF forum database Coding: Hvordan at hente brugerens profil data med SSI.php fra en SMF forum database












































