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 Updates via RSS | E-post Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: Send function as… Avaleht / kodeerimise / JavaScript ja Ajax / JavaScript: Saada toimida ...

JavaScript: Send function as a parameter to another function (callbacks) Javascript: Saada funktsiooni parameeter teise funktsiooni (kutse,)

Posted on 29. Postitatud 29. Jul, 2009 by Dragos in Coding , JavaScript & Ajax Juuli, 2009 Dragos in kodeerimise, JavaScript ja Ajax

I'm sure you've seen a lot of code where functions are send as parameters, usually working as function callbacks eg: Olen kindel, et oled näinud palju koodi kui funktsioonid on saadavad nagu parameetrid, tavaliselt töötab funktsiooni kutse, nt:

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

But how does the function setTimeout execute the passed function as a parameter? Aga kuidas toimida setTimeout ellu möödunud funktsiooni parameetrina? The answer is simple, but I need to provide you an example to understand it Vastus on lihtne, kuid ma pean teile näiteks seda mõista ;)

Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. Loome lihtne funktsioon, vastu kaks parameetrit: esimene tõeväärtuse teine - funktsiooni. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. Meie ülesanne on analüüsida tõeväärtus parameetrit ja juhul, kui väärtus on tõene, funktsiooni edasi nagu parameeter olema sooritatud.

 function simpleFunc(bool,func) { funktsiooni 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. Nagu te teate, lisame sulgudesse nime järel teine parameeter, kuna me käsitleme seda funktsiooni. And that's te whole secret. Ja see on te kogu saladus.

Now that is how we will use the simpleFunc function: Nüüd see, kuidas me kasutame simpleFunc funktsioon:

 simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false simpleFunc (valesid, function () (alert ( "Yupee!");); / / see ei teate midagi, sest tõeväärtus parameeter on vale 
 //example two / / näiteks kaks 
 simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (tõsi, function () (alert ( "Yupee!");)); / / see teade Yupee! :)

Wish you luck! Soovin teile õnne!

Translate this post Tõlgi see postitus


Related posts: Seonduvad postitused:

  1. Javascript: How to validate email address with JavaScript? Javascript: Kuidas kinnitada e-posti aadress JavaScript?
  2. JavaScript: How to get the index (position within a group) of an object with jQuery? Javascript: Kuidas saada indeks (seisukoha rühma) objekti jQuery?
  3. JavaScript: What if jQuery animation doesn't fire/start? Javascript: Mida teha, kui jQuery animatsioon ei tule alustada?
  4. Coding: How to get code suggestions and function completion in Netbeans? Kood: Kuidas koodi ettepanekud ja funktsiooni lõpetamist netbeans?
  5. Coding:How to fetch user profile data with SSI.php from a SMF forum database Kood: Kuidas tõmmata kasutajaprofiili andmete SSI.php alates SMF foorumi andmebaas

    blog comments powered by Disqus blog comments powered by Disqus