-2

Okay, so I have a HUGE array with 10,000+ strings and I want the loop through an array one string at a time, like I want to wait until the function is done to move on to the next string in the array.

Right now the loop puts strings as fast as it can through my function which I can't have because these strings are inserted into a $.get request... And it makes WAY too much requests at a time...

Here's my code currently:

var sp = ["48343", "48383", "48934893", "438943", "47849345", "45843945", "47483923", "38445"];
for (var i = 0; i < sp.length; i += 1) {
    check(sp[i]);
}

and please forgive me if I didn't explain good enough, instead of voting down kindly ask me what to explain, thanks :D

10
  • 4
    Depends on how many requests you want to make at once. You'll have to implement a throttle algorithm. (but, 10,000+ requests, even throttled, doesn't sound like a good design strategy) Commented Dec 14, 2018 at 1:14
  • or just plain sleep function.. Commented Dec 14, 2018 at 1:15
  • 3
    Making 10,000 requests in series doesn't seem to be a good idea either. Are you controlling the server the request is sent to? If yes, change it to accept multiple IDs. Commented Dec 14, 2018 at 1:17
  • 1
    Why can't you post the array and write some server code to process that array instead? Commented Dec 14, 2018 at 1:22
  • 1
    You don't have to send all the data at once if that's a concern, but you should at least make the server be able to process multiple values at a time. Commented Dec 14, 2018 at 1:29

3 Answers 3

0

From what I can tell, you may want to simply use a setInterval to space out your requests.

Example:

var sp = ["...", "..."];
var i = 0;
var interval = setInterval(function() {
  if (i >= sp.length) {
    clearInterval(interval);
  } else {
    check(sp[i++]);
  }
}, 5);
Sign up to request clarification or add additional context in comments.

Comments

0

You need to send the whole array to the server in one request and do the check on the whole array on the server. Then the response from the request is an array of the ones that pass the check. Your current design is horrible, please don't do thousands of get requests.

Comments

-1

By default, $.get() is asynchronous.

Check below links

So it's better if you make your calls synchronous (but the problem is it has been deprecated since jQuery 1.8) just by introducing a new property async with value false to the object passed for call.

But it is better if do not use that if you're using jQuery version >= 1.8.

If not, you can check the below links:

Note » In this way the calling statement inside for loop will wait for the completion of the request i.e. once the function returns, then next iteration will continue.

Finally, I think, using Web workers with async ajax get request will be a better choice for your application.

Check https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers.

1 Comment

async: false is deprecated, both in standard Javascript and in jQuery, in addition to being a bad idea in general. Please don't recommend it - better for JS learners to work with asynchronity instead, it's a very large part of the language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.