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 अद्यतन Via आरएसएस | ईमेल Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: Send function as… घर / कोडिंग / जावास्क्रिप्ट और Ajax / जावास्क्रिप्ट: समारोह के रूप में भेजें ...

JavaScript: Send function as a parameter to another function (callbacks) जावास्क्रिप्ट: एक और समारोह callbacks (एक पैरामीटर के रूप में कार्य भेजें)

Posted on 29. 29 पर तैनात हैं. Jul, 2009 by Dragos in Coding , JavaScript & Ajax जुलाई, 2009 कोडिंग, जावास्क्रिप्ट और Ajax में Dragos द्वारा

I'm sure you've seen a lot of code where functions are send as parameters, usually working as function callbacks eg: मुझे यकीन है कि आप कोड का एक बहुत कुछ है, जहां कार्य मानकों के रूप में भेज रहे हैं आम तौर पर समारोह जैसे callbacks के रूप में काम कर देखा है हूँ:

setTimeout(function () { alert('test'); },1000); setTimeout समारोह (() (( 'परीक्षण' सावधान);), 1000);

But how does the function setTimeout execute the passed function as a parameter? लेकिन यह कैसे काम करता है setTimeout एक पैरामीटर के रूप में पारित समारोह को अंजाम? The answer is simple, but I need to provide you an example to understand it जवाब आसान है, लेकिन मैं आपको एक उदाहरण से समझ प्रदान करने की आवश्यकता ;)

Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. चलो एक सादे समारोह बनाने के लिए, दो मापदंडों को स्वीकार: पहले एक बूलीयन मान, दूसरा - एक समारोह. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. हमारे समारोह बूलीय पैरामीटर का विश्लेषण और मामले में मूल्य सही है, एक पैरामीटर के रूप में पारित समारोह है निष्पादित किया जाएगा.

function simpleFunc(bool,func) { समारोह simpleFunc (bool, समारोह) (
    if(bool) func(); अगर (bool) समारोह ();
} )

As you notice, we add parentheses after the name of the second parameter, because we'll treat it as a function. जैसा कि आप देख, हम दूसरा पैरामीटर के नाम के बाद कोष्ठकों जोड़ने के लिए, क्योंकि हम इसे एक समारोह के रूप में देता हूँ. And that's te whole secret. और है कि ते सारा रहस्य.

Now that is how we will use the simpleFunc function: अब है कि हम simpleFunc समारोह का उपयोग कैसे करेंगे:

simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false simpleFunc झूठी (समारोह () ( '(Yupee सावधान!');), / / यह कुछ भी सचेत नहीं है, क्योंकि बूलीय पैरामीटर झूठी है
//example two / / उदाहरण के दो
simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc सच (समारोह () ( '(Yupee सावधान!');)); / / इस Yupee चेतावनी होगा! :)

Wish you luck! भाग्य तुम काश!

Translate this post अनुवाद इस पोस्ट


Related posts: संबंधित पोस्ट:

  1. Javascript: How to validate email address with JavaScript? : जावास्क्रिप्ट कैसे जावास्क्रिप्ट के साथ ईमेल पते की पुष्टि के लिए?
  2. JavaScript: How to get the index (position within a group) of an object with jQuery? : जावास्क्रिप्ट कैसे सूचकांक प्राप्त करने की स्थिति एक समूह के भीतर (jQuery के साथ एक वस्तु का)?
  3. JavaScript: What if jQuery animation doesn't fire/start? जावास्क्रिप्ट: क्या होगा यदि jQuery एनीमेशन नहीं आग / शुरू कर दिया?
  4. Coding: How to get code suggestions and function completion in Netbeans? : कोडिंग कैसे कोड सुझाव और Netbeans में कार्य पूरा पाने के लिए?
  5. Coding:How to fetch user profile data with SSI.php from a SMF forum database : कोडिंग कैसे एक SMF मंच डेटाबेस से SSI.php के साथ उपयोगकर्ता प्रोफ़ाइल डेटा लाने के लिए

    blog comments powered by Disqus ब्लॉग टिप्पणियों Disqus द्वारा संचालित