Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6863781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:49:43+00:00 2026-05-27T02:49:43+00:00

I am loading multiple scripts of a plugin via get $.getScript , and I

  • 0

I am loading multiple scripts of a plugin via get $.getScript, and I should only initiate the plugin after the last load of the script, but how can I know when the loop reaches the last one?

// load multiple files.
var scripts = [
    'mode/xml/xml.js',
    'mode/css/css.js',
    'mode/javascript/javascript.js'
];

alert(scripts.length); // 3

for(var i = 0; i < scripts.length; i++) 
{
    $.getScript(http_root+'global/views/javascripts/codemirror/original/'+scripts[i], function() {

        alert(i); // this will alert number 3 for 3 times.

        if(i == scripts.length) // this means that the plugin will be initiated 3 times.
        {
            var editor = CodeMirror.fromTextArea(document.getElementById("code_content_1"), { 
                mode: "text/html", 
                theme: "eclipse",
                lineNumbers: true
            });
        }
    });

    alert(i); // this will alert the number from 0 to 2.
}

any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:49:44+00:00Added an answer on May 27, 2026 at 2:49 am

    Wrap call in closure so that variable i wont change on every iteration.

    for(var i = 0; i < scripts.length; i++) 
    {
      (function(i) {
          $.getScript(http_root+'global/views/javascripts/codemirror/original/'+scripts[i], function() {
    
          if(i + 1 == scripts.length)
          {
            var editor = CodeMirror.fromTextArea(document.getElementById("code_content_1"), { 
                mode: "text/html", 
                theme: "eclipse",
                lineNumbers: true
            });
          }
        });
      })(i);  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After loading a PHP template (using jQuery's load function), this simple script won't make
Is it possible to load multiple scripts with same variables/functions in JS, without overriding
I have tried multiple times, but never have been able to install scipy via
What is the best way to design a SSIS package? I'm loading multiple dimensions
Try loading this normal .jpg file in Internet Explorer 6.0. I get an error
Error loading script occurs under FireFox 3 if I quickly click different links on
After loading a reference to an assembly with something like: import clr clr.AddRferenceToFileAndPath(r'C:\foo.dll') How
After loading dynamic content to a DIV, I'd like to add a Close option,
I am working on a project where there's multiple libraries loading in the head.
I've a page in an ASP.net site where i've multiple content placeholders, script tags

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.