0

I have a 2D array which has a bunch of arrays - let's call them inner-arrays. Each inner array has a bunch of URL strings and an associated time-interval value. In other words, all the URL's in a single inner-array has the same time-interval value.

The Javascript sends an AJAX request for each of the inner-arrays at regular intervals, and that interval is actually the aforementioned time interval; and then on the server side some processing is done on the URL's in the inner-array passed, and a result is returned to the Javascript. On the basis of this response, some change is made in the DOM.

And this process goes on forever.

Here is my problem: When I load this program in the browser, the browser keeps running and the page doesn't get loaded fully. But AJAX is supposed to be Asynchronous, isn't it? What am I doing wrong here?

for (var innerArrayKey in outerArray) {
  var innerArray = outerArray[innerArrayKey];

  setInterval(function() {
    $.ajax({
      url: "url/of/my/server/script",
      method: "post",
      data: {innerArray:innerArray},
      success: function(...) {
        // MAKE SOME CHANGE IN DOM
      },
      error: function(...) {
        ...
      }
    });
  }, innerArray[0]["intervalValue"] );
}
JBel
  • 329
  • 1
  • 5
  • 19
  • 1
    The main problem is that `innerArray` isn't what you think it is as of when the `setInterval` callback runs; see the linked question and its answers for why and how to fix it. – T.J. Crowder Aug 07 '18 at 14:16
  • 1
    Separately, though, using `setInterval` with an asynchronous process that may take varying amounts of time (e.g., ajax) is a recipe for chaos. Instead, uset `setTimeout` and start the next iteration when you get the ajax callback completing the previous iteration. – T.J. Crowder Aug 07 '18 at 14:16
  • @JonasW. I really did try and wrote yours in my code as well – JBel Aug 07 '18 at 14:32
  • @T.J.Crowder I just started reading the other SO question you linked to. About your 2nd comment: My ultimate goal is to monitor a bunch of URL's at regular intervals (there's a time interval defined for each URL), and I do that by sending AJAX requests to the server where the monitoring and other processing is done. To be continued... – JBel Aug 07 '18 at 15:57
  • @T.J.Crowder Continuing from previous comment: So to decrease the number of AJAX requests I have grouped the URLs such that those with the same time-interval defined go into the same group, and then I send a group in one AJAX request. _NOW, CONCEPTUALLY SPEAKING, the monitoring process for all the URL's should start at the same time (especially because we have a time interval setting defined for each URL, after which the monitoring process should be repeated). Isn't it?_ – JBel Aug 07 '18 at 16:01
  • 1
    The ajax process will take a varying period of time, potentially longer than the interval timer. So best practice is to start the timer for the next iteration when the current iteration completes (e.g., in the ajax completion callback). – T.J. Crowder Aug 07 '18 at 16:20
  • @T.J.Crowder Suppose I have a hundred url's, and interval for some is set at 3 days where as for some it is set at few hours and for some it is set as 2 or 3 minutes. Then some URL's will start to be monitored after hours or days. My requirement is to monitor all of the url's at the same time. – JBel Aug 07 '18 at 16:46
  • T.J.Crowder Is there _any_ way I can _start_ monitoring all the url's at the same time and then the ajax requests are repeated at their predefined intervals? – JBel Aug 07 '18 at 16:50
  • @T.J.Crowder Or do you think I am better off ditching the _Web app_ altogether and write a desktop application in Java where I can easily create a separate thread for the group of url's having the same time interval? – JBel Aug 07 '18 at 16:55
  • 1
    It's not complicated. Again, you just use `setTimeout` instead of `setInterval`, and in the `complete` handler, use `setTimeout` again to schedule the next execution. (That said: Monitoring other URLs from a browser-hosted page will only work if the other URLs are on the same origin or allow cross-origin requests to them.) – T.J. Crowder Aug 07 '18 at 17:03
  • @T.J.Crowder Yes, the implementation is not the problem (Thanks to JonasW. I learnt about that) but here is the problem: suppose the interval for the first url is _1 hour_ and that for the second URL is _1 day_. So the second url will actually start to be monitored _1 hour after the program starts running,_ and the second one starts to be monitored _1 day and 1 hour after the program starts running._ So the problem is that the time-intervals won't be respected here. – JBel Aug 08 '18 at 01:57
  • I am coming from a Java background, and I need the effect of sending the requests for each of the `outerArray`s in a separate thread, and when the response comes inside that worker thread, it manipulates the DOM, which would be like the main thread, being accessed by the worker threads to manipulate the DOM. @T.J.Crowder – JBel Aug 08 '18 at 03:47
  • 1
    There's no problem. Just use one hour for the one URL and one day for the other. You also don't need separate threads, all of the work is done asynchronously, the actual time your code runs will be milliseconds at a time while the ajax work happens off-thread. – T.J. Crowder Aug 08 '18 at 08:15
  • @T.J.Crowder Sorry, after being distracted, I got back and tried to implement it. Can you take a [look here](https://stackoverflow.com/questions/51874037/calling-settimeout-for-all-members-in-object-never-called-for-1st-member-an) as I am running into unexpected behavior? I posted it as a separate question because this question is about _how_ that is looking for an idea, and that one is about _what_ that is problems in implementation after selecting an idea. – JBel Aug 16 '18 at 09:56

0 Answers0