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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:18:08+00:00 2026-06-18T10:18:08+00:00

I have been working on a pretty big javascript function, and when I finally

  • 0

I have been working on a pretty big javascript function, and when I finally thought it was done, it stopped working completely.

Here it is:

function getLinksFrom(title) {
  var returnArray = [],
      plcontinue = '',
      url = 'http://en.wikipedia.org/w/api.php?action=query&prop=links&titles=' + title + '&format=json&pllimit=500&plnamespace=0&callback=?';
  while (returnArray.length === 0 || plcontinue !== '') {
      if (plcontinue !== '') {
          url = 'http://en.wikipedia.org/w/api.php?action=query&prop=links&titles=' + title + '&format=json&pllimit=500&plnamespace=0&plcontinue=' + plcontinue + '&callback=?';
      }
      $.ajax({url: url, 
          dataType: 'json',
          success: function(data) {
              console.log(data);
              for (key in data['query']['pages']) {
                  links = data['query']['pages'][key]['links'];
              }
              for (var i = 0; i < links.length; i += 1) {
                  returnArray.push(links[i]['title']);
              }
              if (data.hasOwnProperty('query-continue')) {
                  plcontinue = data['query-continue']['links']['plcontinue'];
              } else {
                  plcontinue = '';
              }
          }
      });
  }
  return returnArray;
}

from what I can tell, it must be getting stuck in the while loop, as the page just freezes up. But with each loop, returnArray grows, and I’m testing it with something that won’t set plcontinue to anything.

Any idea what is going wrong? Possibly something with asynchronous loading, I’m new at that.

EDIT: So I’ve figured out via helpful comments that it’s looping again and again making more and more requests without waiting for the ajax to finish before starting again. How can I stop it from doing that?

  • 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-18T10:18:10+00:00Added an answer on June 18, 2026 at 10:18 am

    The key is that AJAX is asynchronous. That means that when JS needs to perform request, it sends it and goes on, continuing to execute the rest of the code. When request is done, callback is fired.

    So in your code it would works like that:

    1. Initialization of variables
    2. While loop condition check. Condition is true, so go to the loop body.
    3. Executing ajax request. We send request and go on.
    4. Again check condition. Since nothing changed (request haven’t any time to complete), condition is still true.
    5. And so on.

    Eventually, maybe, one of dozen of request will end, modify returnArray and plcontinue, but this could take long time because what you have in your function body is, basically, infinite (almost) loop, which takes most of CPU resources.

    The solution is to replace iteration with recursion: instead of iterating in infinite loop untill success, you can recurse until success. Delete while loop from getLinksFrom and add getLinksFrom() to the ajax request callback.

    Another approack is to do that request synchronous. There is a special flag for it.

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

Sidebar

Related Questions

I have been working on a site that makes some pretty big use of
I have been working on a project in C# (.net4). Project pretty much allows
I have been working in a big program and one of its functionalities should
So I have been working on a Clojure tutorial and it's pretty fun so
I have been working in ZF since it was v1.6 and i am pretty
So, I have been working on adding BitmapImages to a ListBox for pretty much
I have been working with the Trello API (pretty neat, btw), but I can't
I have been working on Java Swing project. Its design is pretty poor. I
Lately I have been working on OS X. Things were going pretty peachy for
I have been working with android for a little while now and feel pretty

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.