开发者

how to minimize the code in jquery with many function in sequence by a loop or something maybe by utilizing arrays

开发者 https://www.devze.com 2023-03-24 09:41 出处:网络
i\'m very new to jquery, please help me in makeing a for loop or something through which i can minimize the amount of code written below :

i'm very new to jquery, please help me in makeing a for loop or something through which i can minimize the amount of code written below :


                $.getScript(basejq + 'ui/jquery.ui.core.js', function(){
                    $.getScript(basejq + 'ui/jquery.ui.widget.js', function(){
                        $.getScript(basejq + 'ui/jquery.ui.mouse.js', function(){
                            $.getScript(basejq + 'ui/jquery.ui.slider.js', function(){
             开发者_StackOverflow中文版                   $.getScript(basejq + 'time.js', function(){
                                    $('.timepicker').jtimepicker();
                                });
                                $('*').remove(':time');
                            });
                        });
                    });
                });

I have no idea, i will appreciate any of your help, thanks in advance.


Here is a cleaner solution but you could go further with this. I am assuming that each one of those scripts is dependent on the previous script.

var scripts = [
    basejq + 'ui/jquery.ui.core.js',
    basejq + 'ui/jquery.ui.widget.js'
    // more files here
], current = 0;

function getScript() {
    if(current >= scripts.length) {
        return;
    }
    $.getScript(scripts[current],
                function() {
                    current += 1;
                    getScript();
                });
}

Also, there are a few libraries that can handle javascript dependencies for you. You may want to look into that.

0

精彩评论

暂无评论...
验证码 换一张
取 消