0

Occasionally my app needs to populate multiple content areas via simultaneous jQuery .load() calls.

Despite the content areas loading in under 100ms, the more simultaneous requests in place, the slower the content is displayed on the document. Sometimes this can be as long as 10-15 seconds for 6 content areas.

Loads are initiated as follows:

$("#MyDiv1").load("/My/Controller/Action/1");
$("#MyDiv2").load("/My/Controller/Action/2");
$("#MyDiv3").load("/My/Controller/Action/3");
...

Any suggestions on how to combat this bottleneck would be appreciated.

Nick
  • 5,844
  • 11
  • 52
  • 98

3 Answers3

1

Thats because of the way javascript works. It wont wait to finish loading content 1 before it starts to load content 2 and 3 so it will all start to load at the same time.. So you should create some kind of que.

something like:

load array = new array()
array[array.length+1]= "/My/Controller/Action/1";  
array[array.length+1]= "/My/Controller/Action/2";  
array[array.length+1]= "/My/Controller/Action/3";  
loadContent();   
function loadContent()    
{
$("#MyDiv").load(array[0] function(){
        array.shift();
       if(array.length>0)
          loadContent() ; 
    });
}

note that is is not tested but it should give you a good ideer of what you can do

Kimtho6
  • 6,154
  • 9
  • 40
  • 56
1

Try to cascade the calls

$('#div1').load('...', function() {
    $('#div2').load('...', function() {
        $('#div3').load('...', function() {
            ....
        });
    });
});
devnull69
  • 16,402
  • 8
  • 50
  • 61
1

ASP.NET handles one request at a time under the same Session. So the second request won't run until the first one has completed. That's to avoid threading issues. You should see some improvement if you use Sessionless controllers.

Check out this link:

What are some scenario's of having a Session-less Controller in ASP.NET MVC3?

Community
  • 1
  • 1
epignosisx
  • 6,152
  • 2
  • 30
  • 31
  • This is very helpful. I do utilise sessions in my application (I have a managed SessionManager static which holds some app wide variables) - is there an alternative? – Nick Jan 10 '12 at 13:17
  • Would changing the Controller into an AsyncController address this problem? – Nick Jan 10 '12 at 13:43
  • @Nick do these specific calls use Session? If not, try moving them to a Sessionless controller. Are they modifying data? If not you can mark the sessionless controller as ReadOnly. – epignosisx Jan 10 '12 at 13:58
  • @Nick AsyncController won't do it. It just delegates the work to a separate thread pool freeing the IIS thread pool to handle other requests. The session issue still occurs. – epignosisx Jan 10 '12 at 13:59
  • I have decorated my controller with [SessionState(SessionStateBehavior.ReadOnly)] as the session variables only need to be read and not written to. Should I continue to inherit from Controller or is there any value in changing to AsyncController so the work takes place on another thread? – Nick Jan 10 '12 at 14:16