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 9004069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:49:08+00:00 2026-06-16T00:49:08+00:00

I have the following script which does not work properly: function getWidgetContent( widget )

  • 0

I have the following script which does not work properly:

function getWidgetContent( widget ) {
    if(widget.script!=null){
        $global_widget_id = widget.widget_id;
        $.getScript( "js/" + widget.script, function() {
            $( ".widget_header_title_" + widget.widget_id ).append( widget.title );
        });
    }
}

This is called as follows:

for ( j = 0; j <= widget_data.d.length - 1; j++ ) {
    getWidgetContent( widget_data.d[j] );
}

I thought this would run the function, within the function, assign a value to the global value, then run the $.getScript per iteration of the loop. But that is not happening. It seems to iterate through the loop until the loop is finished, lets say it loops 3 times, assign a value to the global value each time, i.e. 3 times, and then it finally goes and does the $.getScript.

Ofcourse that will not working as it will now only use the last assignment of the global value in the $.getScript file 3 times…

How do I change this so it assigns a value to the global variable, runs the $.getScript. When done, continue with the original loop, assign the next value to the global variable, run the $.getScript until the loop has finished.

  • 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-06-16T00:49:09+00:00Added an answer on June 16, 2026 at 12:49 am

    You’re getting this because $.getScript is an asynchronous method. In this case, it means that the method returns immediately before the script has finished loading, and continues executing code after it.

    What this means is that something like:

    $.getScript('a.js', function () {
        console.log('Loaded a');
        }); 
    $.getScript('b.js', function () {
        console.log('Loaded b');
        }); 
    $.getScript('c.js', function () {
        console.log('Loaded c');
        }); 
    // Output could be:
    // Loaded c
    // Loaded a
    // Loaded b
    

    This means that all of the script files requests can be done simultaneously but it also means that the order is not deterministic (fixed).

    Use Promises

    You can ensure that the getWidgetContent is called sequentially by chaining promises if you are using jQuery 1.5 and above.
    However, the pitfall of this method is that you will not be concurrently loading all the script requests at the same time, the requests will be sent one after another after the previous one has completed.

    Make sure you return the result of $.getScript which is a Deferred Object (I made minimal changes here, just note the return statements):

    function getWidgetContent( widget ) {
      if(widget.script!=null){
        $global_widget_id = widget.widget_id;
        return $.getScript( "js/" + widget.script, function() {
            $( ".widget_header_title_" + widget.widget_id ).append( widget.title );
            });
      }
      return null;
    }
    

    New method to perform a new getWidgetContent after a previous promise is fulfilled (completion of previous action):

    function doGetWidgetContentAfter(promise, widget) {
      return promise.then(function () {
          return getWidgetContent( widget );
          });
    }
    

    Calling it:

    var promise = $.when(true);
    for ( j = 0; j <= widget_data.d.length - 1; j++ ) {
      promise = doGetWidgetContentAfter( promise, widget_data.d[j] );
    }
    

    Explanation

    What doGetWidgetContentAfter does is that when the said promise is complete then call the getWidgetContent function for widget. The then method returns a promise that completes when any internal methods promise completes.

    I know it’s starting to sound complicated, but do play around and experiment with it. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following script which does not work 100%, it returns about 20
I have the following script which does not work. it should be using jquery
I have the following call which simply does not work in ie7 or 8
I have the following function in a PHP script, which returns and API call:
I have the following PHP script to hash a user's password which it does,
I have the following script which won't work when executed as a script, but
I have the following script which I need to combine together somehow. Each function
I have the following script which first shows only the first para of the
I have the following script which appears multiple times on my page. I have
I have the following script which will delete a file off a BlackBerry. However,

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.