I have Action, that returns data for charts on page. It takes chartType parameter and returns charts data. Page uses jQuery and $.post
function to call for it. Here is example:
var fillChart = function (chartData){ /* fill routine here */};
$(function () {
$.post(
'/Charts/ChartsJson',
'chartType=Type1',
fillChart,
'json');
$.post(
'/Charts/ChartsJson',
'chartType=Type2',
fillChart,
'json');
// other calls here [*]
});
I have array of chartTypes, that should be processed. If I add all calls after commented line, then they all will be sent at one time. It's ok, if there are from 1 to 3 items. But if there are more items, then it's bad for performance.
Is there any way to restrict amount of simultaneously executed post requests开发者_JAVA技巧? I know, that I could use callback of first post, which would invoke second, and so on... My question is: "Is there any other way?"UPD: Each call is already partitioned. (And amount of items in my array is not always known.) This action return about 1000 complex elements per call, and different chartType
says which table to use (and what logic for extraction data)
I have had good success setting up an ad-hoc "message queue". The messages can be json objects with the parameters for the posts as the data. You can have any number of workers pulling objects off the queue.
For example:
var ajax_message_queue = [];
ajax_message_queue.push({
"ajaxtype" : "post",
"action" : '/Charts/ChartsJson',
"data" : handlers.fillChart,
"datatype" : "json"
});
A worker would simply be a setTimeout based function that pops off the oldest message, and executes it. The worker can then be blocked from popping the next message until the first is complete. If you want the worker can even be made to redo the request if it fails.
This is done automatically by the browser -- a web browser will not send more than a certain number of HTTP requests to any single server at any one time. They will be automatically queued and executed when complete.
I would suggest rewriting your serverside code to allow the submission of multiple items in one go, if you want to improve performance.
You should probably send all data at once. Re-structure your input array to have a chartType
key. That way in backend you could proccess it accordingly
精彩评论