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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:48:19+00:00 2026-06-15T19:48:19+00:00

I’m trying to get some javascript to run a for loop which will create

  • 0

I’m trying to get some javascript to run a for loop which will create an xml file and then, after the loop is complete, to visit a link that would allow the user to download the file. Currently what happens is that it gets half-way through creating the file and then sends the user the link to download the file.

I followed this question’s advice and created a callback variable and moved the download link to another function, however that hasn’t solved the problem either. Does anyone else have any idea how to do it? My code is:

var callbackcount=0;
function populateTemplate(numResults) {
  for (i=1; i < numResults; i++) {
    $.post('linkToTheFileThatCreatesTheXMLFile', {WithSomeParameters};
    download(numResults);
  }
}

function download(numResults) {
  callbackcount++;
  if (callbackcount == numResults) {
    window.location.href="linkToPHPThatDownloadsFile";
  }
}

Thanks heaps

Edit:
Console.log results:

LOG: 230 LOG: 330 LOG: 230 LOG: 330 
LOG: 430 LOG: 530 LOG: 630 LOG: 730 
LOG: 830 LOG: 930 LOG: 1030 LOG: 1130 
LOG: 1230 LOG: 1330 

If numResults = 7

LOG: 27 LOG: 37 LOG: 47 

or

LOG: 27 LOG: 37 LOG: 47 LOG: 57 

etc.

Edit:
The new code:

function populateTemplate(numResults) {
  var callbackcount = 1;
  for (i=1; i < numResults; i++) {
    $.post('linkToTheFileThatCreatesTheXMLFile', {WithSomeParameters}, function() {
      callbackcount++;
      console.log(callbackcount, numResults);
      if(callbackcount === numResults - 1) {
        window.location.href="linkToPHPThatDownloadsFile";
      }
    });
  }
}
  • 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-15T19:48:20+00:00Added an answer on June 15, 2026 at 7:48 pm

    $.post is asynchronous call. So by the time you get to the last loop, where in the closure callbackcount will equal numResults all the posts won’t have gone through yet. definitly not the last one.

    $.post allows for a callback which is called once the post is complete. This callback should call your download methods.

    var callbackcount=0;
    function populateTemplate(numResults) {
      for (i=1; i < numResults; i++) {
        $.post('linkToTheFileThatCreatesTheXMLFile', {WithSomeParameters}, function() {
    
          callbackcount++;
          if (callbackcount === numResults) {
            window.location.href="linkToPHPThatDownloadsFile";
          }
    
        });
      }
    }
    

    This way you evaluate the function when ever the post is complete. Also I inlined it to make the access of the variables a little esier.

    BTW: always use === over == in JavaScript See why

    EDIT

    The loop index i should start at 0. Also it should be declared as a local variable, not global.

    for ( var i = 0; i < numResults; i++ )

    Otherwise the loop will only run 5 times with a numResults of 6.

    EDIT 2 – Second approach

    If it has to do with too many requests per frame (which I doubt) You could also try this and see if it makes a difference.

    var callbackcount=0;
    function populateTemplate(numResults) {
      for (i=1; i < numResults; i++) {
    
        setTimeout( function() {
    
          $.post('linkToTheFileThatCreatesTheXMLFile', {WithSomeParameters}, function() {
    
            callbackcount++;
              if (callbackcount === numResults) {
                window.location.href="linkToPHPThatDownloadsFile";
              }
    
          });
    
        }, 100);
      }
    }
    

    this delays every request and therefore has not every request going in one frame.

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

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words
I'm trying to select an H1 element which is the second-child in its group
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.