javascript callback after loop

Posted by RobertPitt on Stack Overflow See other posts from Stack Overflow or by RobertPitt
Published on 2010-06-13T00:59:59Z Indexed on 2010/06/13 1:02 UTC
Read the original article Hit count: 388

Filed under:
|
|
|

Hey guys,

Iv'e just started a new Wordpress blog and i have started to build the JavaScript base!

the issue im having is funnction a fucntion that loops several variables and includes the required JS libraries, what i need to do is execute the callback when the loop is finished!

Heres my code!

var Utilities = {
    Log : function(item)
    {
        if(console && typeof console.log == 'function')
        {
            //Chrome
            console.log(item);
        }
    },
    LoadLibraries : function(callback)
    {
        $.each(Wordpress.required,function(i,val){
            //Load the JS
            $.getScript(Wordpress.theme_root + '/js/plugins/' + val + '/' + val + '.js',function(){ //  %/js/plugins/%/%.js
                Utilities.Log('library Loaded: ' + val);
            });
        });
        callback();
    }
}

And the usage is like so!

Utilities.LoadLibraries(function(){
    Utilities.Log('All loaded');
});

Above you see the execution of callback() witch is being executed before the files are in the dom! i need this called at the end of every library inclusion!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about dom