-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

Fallen
  • 1
  • 3
  • 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) – CertainPerformance Dec 14 '18 at 01:14
  • or just plain [sleep function](https://stackoverflow.com/a/39914235/4648586).. – Bagus Tesa Dec 14 '18 at 01: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. – Felix Kling Dec 14 '18 at 01:17
  • @FelixKling the problem is it's sending too many requests at a time... It sends so much that my server stops me from accessing my site for mins (ratelimited) – Fallen Dec 14 '18 at 01:19
  • 1
    Why can't you post the array and write some server code to process that array instead? – Andy Dec 14 '18 at 01:22
  • and @FelixKling my server handled 5,000+ requests in series just fine... So how is it not a good idea? I made a username gen and it made 5,000+ requests and handled them just fine (it didn't use a foreach loop) – Fallen Dec 14 '18 at 01:23
  • @Andy wdym?... Php would be terrible to process that. The page would never load. – Fallen Dec 14 '18 at 01:25
  • Any network connection involves some kind of overhead (e.g. handshake). Making X requests means you incur that overhead X times. Lets say the handshake takes 5ms, then doing 10,000 handshakes will take 50 seconds! That doesn't even include sending and receiving data yet. And even if the server can handle a large amount of requests, it will still need to use fewer resources if you increase the payload size instead. – Felix Kling Dec 14 '18 at 01:28
  • 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. – Felix Kling Dec 14 '18 at 01:29
  • Or what if you use Web workers? – hygull Dec 14 '18 at 01:41

3 Answers3

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);
Brenden
  • 199
  • 10
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.

Adrian Brand
  • 15,308
  • 3
  • 24
  • 46
-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.

hygull
  • 7,072
  • 2
  • 35
  • 42
  • 1
    `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. – CertainPerformance Dec 14 '18 at 02:07