-1

OK, I've been looking around to solve this problem, but since JavaScript is single-threaded I'm not sure if it's even possible. Do you guys know if there's an alternative?

Here's what I am trying to accomplish:

I'm making ten asynchronous requests to get data from a server. I want to continue execution of my logic once all ten responses are received, or after 4000 ms have elapsed.

If I was using java I can simply fire ten different threads and have my main thread sleep. Once all ten responses are received or the 4000 ms elapsed, then I can interrupt the thread and continue execution. I just have not idea how to do this in JavaScript.

Any ideas?

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • Are you open to jQuery? jQuery's Deferred objects might be of use. You can say things like: `$.when(ajaxCallOne(), ajaxCallTwo()).done(function() { 'all done!' });`. The code after this line continues to run and the `done()` callback fires only when both calls are finished (unless one fails). FYI: Your browser is not doing 10 simultaneous AJAX requests; they are usually limited to just a couple at a time. Example: http://www.erichynds.com/blog/using-deferreds-in-jquery – Cᴏʀʏ Jan 08 '14 at 20:29
  • 1
    @Cory if you're suggesting promises I recommend a stronger promise library like [Bluebird](https://github.com/petkaantonov/bluebird) for example that is not only hundreds of times faster but also has a much richer API. – Benjamin Gruenbaum Jan 08 '14 at 20:35
  • 1
    @BenjaminGruenbaum: I was. Thanks for the link about Bluebird, I'll have to check it out. – Cᴏʀʏ Jan 08 '14 at 20:38

2 Answers2

0

In my example I fire Do() every 100ms up to 10 times... this is very basic.

var _timeoutExpired = false;
var _count = 0;

function Do() {

   window.clearInterval( intr);

   if ( _timeoutExpired == false )
   {
   ... your code..
   }
   else{
       window.clearInterval( intr);
       return;
   }

   _count++; 
   if ( _count == 10 ) {
       window.clearInterval( intr);
   }
   else{
       intr = window.setInterval(Do,100 );
   }
}
function Timeout() {  _timeoutExpired = true; }

var  intr = window.setInterval(Do,100 );
window.setTimeout(Timeout,4000);
T McKeown
  • 12,971
  • 1
  • 25
  • 32
  • 2
    Just a small FYI: passing strings to `setInterval` and `setTimeout` is highly discouraged. It's better to use actual function references (e.g. `setInterval(Do, 100)` or `setInterval(function() { Do(); }, 100)`;). – Cᴏʀʏ Jan 08 '14 at 20:34
0

just have something like

var responses = [],
    handleResponses = true;

function do_ajax_calls(){
   //do you ajax calls here and call ajax_callback as their callback
}

function ajax_callback(response){
   if (!handleResponses) return;

   responses.push(response)
   if (responses.length == 10){
       final_callback()
   }       
}

function final_callback(){
   handleResponses = false;
   //do whatever with the existing responses here
}

setTimeout(function(){
   final_callback();
}, 4000)
hobberwickey
  • 6,118
  • 4
  • 28
  • 29