Ajax synchronous sending and string truncation by step

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.