0

I have a for loop that loops a bunch of times (1-1000+). In that loop, i'm calling an ajax page to do some database stuff. I need a way to tell when the last item is done so i can refresh the page. What i did was run the loop and db code and right after do a refresh of the page, but what's happening, is a lot of the requests aren't done because it seems it's refreshing before some db updates are applied. How do i do this? Here's my code:

      for (i = 0; i < acIDs.length; i++) {

        var acID = acIDs[i];

        $.get('/ajax/db.asp?clientID=123&action=batchEditA&batchFieldA='+batchFieldA+'&batchValueA='+escape(batchValueA)+'&acID='+acID+'&pos='+pos);

      }      
location.reload(true);
Damien
  • 4,093
  • 9
  • 39
  • 52
  • 2
    Not at all an answer to the actual question, but if you're making 1000 ajax requests in a loop you probably want to rethink your design. – James Montagne Sep 18 '15 at 19:59
  • Rethink your design, and send the entirety up to the server in one single call, and refresh the page when you get the result. – Robert McKee Sep 18 '15 at 20:01
  • This is the re-think! I was sending the request all in one shot with values and the like and it wasn't working. Query strings can only be so long before the browsers rejects it. This is my way of doing the call 1 at a time in a browser. Any ideas? – Damien Sep 18 '15 at 20:08
  • use `post` instead of `get` – charlietfl Sep 18 '15 at 20:16
  • just wondering, what difference does it make? – Damien Sep 18 '15 at 20:17
  • you already found out that get has character restrictions in url, post sends data in request body not in url – charlietfl Sep 18 '15 at 20:18
  • Tried that as well... no go. Same problem – Damien Sep 18 '15 at 20:21
  • I found a solutions that works for me. I simply, when the request is done, clearTimeout(timer); timer = setTimeout(); That way, the previous request will stop the timer if there is one, and the last request will start it and have nothing to stop it. – Damien Sep 18 '15 at 20:32

0 Answers0