0

I hope this makes sense...

I have a page that loads the same external file 1-12 times depending on the usage. The larger load times takes up to a full minute to load the page so I'm trying to load each file via ajax, but using a loop to load the files completely hangs the server.

Here's the code I'm using so far:

function getSchedule(startday,scheduleID,scheduleView){
  $.ajax({
    type: 'POST',
    url: siteURL+'/includes/schedule-'+scheduleView,
    data: {startday:startday,scheduleID:scheduleID},
    success: function(data){
      $('.scheduleHolder'+scheduleID).html(data).removeClass('loading');
    }
  });
}

var loadSchedules = [];

var startday = $('#the_start_day').text();
var totalSchedules = $('.scheduleHolder').length;
var i = 0;
$('.scheduleHolder').each(function(){
  var currentHolder = $(this);
  var scheduleView = currentHolder.attr('rel');
  var scheduleID = currentHolder.attr('id');

  loadSchedules.push(getSchedule(startday,scheduleID,scheduleView));

  if (totalSchedules==i) {
    $.when.apply($, loadSchedules);
  }

  i++;
});

Each file should only take 2-5 seconds to load when it's loading individually, so I was really hoping the total load time could go from 60 seconds to 10 or so.

So, my question is how can I load the same file multiple times and at the same time without killing the server? Does that make sense?

smplejohn
  • 31
  • 6

1 Answers1

0

I believe you need to use synchronous requests, this hopefully helps:

jQuery: Performing synchronous AJAX requests

(the wording is a bit misleading, read here: Asynchronous and Synchronous Terms )

but I can't vouch what will happen to your server with the 12 simultaneous requests - if the other end is written well, nothing.

Community
  • 1
  • 1
Steve Horvath
  • 508
  • 1
  • 4
  • 10