Ajax synchronous transmission:
(1) Use of closures
Multiple Ajax requests are synchronously sent. (How many Ajax requests can be synchronously sent? Let the browser decide on its own). synchronous transmission, each ajax request handles its own request separately.
For (var I = 0; I <len; I ++ ){
Ajax. post (url, param, function (param, I ){
Return function (txt ){
Var res = txt. evalExp (); // parse the returned json object
// Here I can be used to process every ajax request
}
} (Param, I ));
}
(2) queue
Ps: the connection cannot be found. Tragedy ~
Function Buffer (handler ){
Var queue = [];
Function run (){
Var callback = function (){
If (queue. length> 0 ){
Run ();
}
}
Handler (queue. shift (), callback );
}
This. append = function (task ){
Queue. push (task );
If (queue. length = 1 ){
Run ();
}
}
}
Function Task (item, url, callback ){
This. item = item;
This. url = url;
This. callback = callback
}
Function taskHandler (task, callback ){
Ajax. post (task. url, task. item, function (txt ){
If (task. callback ){
Task. callback (txt );
}
Callback (txt );
});
}
Var buffer = new Buffer (taskHandler );
Operation:
Buffer. append (new Task (ajaxParams [I], ajaxURL [I], ajaxCallback [I]); // parameter, url, callback
String truncation by step
In fact, just use the function directly. Do not use prototype.
In addition:
Chinese characters are particularly difficult to handle,
When Step is 1, all are separated.
When Step is an odd number (3, 5 ...), The steps are separated. For example, the example above is divided