JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Sūtīt funkcija kā parametru citai funkcijai (callbacks)
Posted on 29. Posted on 29. Jul, 2009 by Dragos in Coding , JavaScript & Ajax Jūl, 2009 by Dragos in Kodēšana, JavaScript un Ajax
I'm sure you've seen a lot of code where functions are send as parameters, usually working as function callbacks eg: Es esmu pārliecināts, ka esat redzējuši daudz kodu, kur funkcijas sūtīt parametrus, kas parasti strādā kā funkcija callbacks piemēram:
setTimeout(function () { alert('test'); },1000); setTimeout (function () (alert ( 'test');), 1000);
But how does the function setTimeout execute the passed function as a parameter? Bet kā tas darbojas setTimeout izpildīt nodots funkcijai kā parametrs? The answer is simple, but I need to provide you an example to understand it Atbilde ir vienkārša, bet man ir nepieciešams, lai sniegtu jums piemērs, lai saprastu to ![]()
Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. Let's izveidot vienkāršu funkciju, pieņemot divus parametrus: pirmkārt Būla vērtību, otrajā - funkcijas. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. Mūsu funkcija analizēs boolean parametrs un, ja vērtība ir patiesa, funkcija pieņem, kā parametrs tiks izpildīts.
function simpleFunc(bool,func) { funkcija simpleFunc (bool, funkcijas) ( if(bool) func(); if (bool) funkcijas (); } )
As you notice, we add parentheses after the name of the second parameter, because we'll treat it as a function. Kā jūs novērojat, mēs pievienojam iekavās pēc nosaukuma otrais parametrs, jo mēs to interpretē kā funkciju. And that's te whole secret. Un tas te viss noslēpums.
Now that is how we will use the simpleFunc function: Tagad, ir, kā mēs izmantosim simpleFunc funkcija:
simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false simpleFunc (false, function () (alert ( 'Yupee!');); / / tas nav trauksmes neko, jo boolean parametrs ir nepatiesa //example two / / piemērs divi simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (true, function () (alert ( 'Yupee!');)); / / šī būs trauksmes Yupee!![]()
Wish you luck! Vēlu jums veiksmi!
Related posts: Related posts:
- Javascript: How to validate email address with JavaScript? Javascript: Kā lai apstiprinātu e-pasta adresi ar JavaScript?
- JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Kā nokļūt indekss (amatu grupā) un objekts ar jQuery?
- JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Ko darīt, ja jQuery animācijas nav uguns / sākt?
- Coding: How to get code suggestions and function completion in Netbeans? Coding: Kā saņemt kodu ierosinājumus un darbību pabeigšanas NetBeans?
- Coding:How to fetch user profile data with SSI.php from a SMF forum database Coding: Kā ielādēt lietotāja profila datus ar SSI.php no SMF foruma datubāze












































