最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - javascript inline callback function to separate function - Stack Overflow

programmeradmin2浏览0评论

Why is this code working:

function onCordovaReady() { 
    navigator.globalization.getLocaleName(function (locale) {
        jQuery.i18n.properties({
            name:'message', 
            path:'lang/', 
            mode:'map',
            language:locale.value,
            callback: function(){
                alert(locale.value);
                alert(jQuery.i18n.prop('msg_hello'));
                alert(jQuery.i18n.prop('msg_plex', 'John'));
            }
        });
    });
}

And this one not:

function onCordovaReady() { 
    navigator.globalization.getLocaleName(function (locale) {
        jQuery.i18n.properties({
            name:'message', 
            path:'lang/', 
            mode:'map',
            language:locale.value,
            callback: onLanguageReady(locale)
        });
    });
}

function onLanguageReady(locale) {
     alert(locale.value);
     alert(jQuery.i18n.prop('msg_hello'));
     alert(jQuery.i18n.prop('msg_plex', 'John'));    
}

I want to make the callback in a different function so my code will look cleaner, but couldn't get it to work. The first alert will work (it will display nl_NL), but the second and third alert will output [msg_hello] and [msg_plex].

Many thanks!

Why is this code working:

function onCordovaReady() { 
    navigator.globalization.getLocaleName(function (locale) {
        jQuery.i18n.properties({
            name:'message', 
            path:'lang/', 
            mode:'map',
            language:locale.value,
            callback: function(){
                alert(locale.value);
                alert(jQuery.i18n.prop('msg_hello'));
                alert(jQuery.i18n.prop('msg_plex', 'John'));
            }
        });
    });
}

And this one not:

function onCordovaReady() { 
    navigator.globalization.getLocaleName(function (locale) {
        jQuery.i18n.properties({
            name:'message', 
            path:'lang/', 
            mode:'map',
            language:locale.value,
            callback: onLanguageReady(locale)
        });
    });
}

function onLanguageReady(locale) {
     alert(locale.value);
     alert(jQuery.i18n.prop('msg_hello'));
     alert(jQuery.i18n.prop('msg_plex', 'John'));    
}

I want to make the callback in a different function so my code will look cleaner, but couldn't get it to work. The first alert will work (it will display nl_NL), but the second and third alert will output [msg_hello] and [msg_plex].

Many thanks!

Share Improve this question asked May 29, 2013 at 8:35 Denny BeulenDenny Beulen 1332 silver badges13 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Try with this:

// beginning of code omitted
callback: function(locale) {
    onLanguageReady(locale)
}

it is because you are assigning undefined to the callback property.

You are calling onLanguageReady and assigns that value to the callback method.

The solution is to use another function as callback function which will call the onLanguageReady function as given by @romainberger

function onCordovaReady() { 
    navigator.globalization.getLocaleName(function (locale) {
        jQuery.i18n.properties({
            name:'message', 
            path:'lang/', 
            mode:'map',
            language:locale.value,
            callback: onLanguageReady
        });
    });
}

function onLanguageReady(locale) {
     alert(locale.value);
     alert(jQuery.i18n.prop('msg_hello'));
     alert(jQuery.i18n.prop('msg_plex', 'John'));    
}

will work if the function calls back with locale.

the callback is expecting a function pointer that it can call once the processing is done when you say onLanguageReady(locale) you are actually executing the function and thus assigning the result of the function as the call back in this case the return is nothing thus undefined

发布评论

评论列表(0)

  1. 暂无评论