[Random] A simple JavaScript asynchronous processing event queue

Source: Internet
Author: User

When we need to execute several functions consecutively and these functions will operate on page elements all the time, it is very likely that the page will have a short card, and then all the information will pop up suddenly.

Generally, setTimeout (FN, 0) is used for execution. In fact, for JS setTimeout, especially for IE6, the response time is about 17 milliseconds at the earliest. Therefore, it is set to 0, not significant.

Moreover, this approach is discontinuous and non-coherent. Therefore, I wrote a simple one myself.

 

1 /**
2 * @ author floyd
3 * @ name: asynchronous processing of event queues
4 * @ example var qe = new QueneEnginer (); qe. add (fn, context, arrParam); qe. start ();
5 */
6
7 var QueneEnginer = function (){
8
9 this. Quene = [];
10}
11 QueneEnginer. prototype = {
12 processTime: 20,
13 /**
14 * add events to the queue
15 * @ param {function} fn function object
16 * @ param {object} context object can be empty
17 * @ param {array} the array of arrParam parameters can be empty.
18 */
19 add: function (fn, context, arrParam ){
20
21 this. Quene. push (
22 {
23 fn: fn,
24 context: context,
25 param: arrParam
26}
27 );
28 },
29 start: function (){
30 var that = this;
31 setTimeout (function () {that. process () ;}, that. processTime );
32 },
33 process: function (){
34
35 var quene = this. Quene. shift ();
36
37 If (! Quene) return;
38
39 quene. FN. Apply (quene. Context, quene. Param );
40
41 quene = NULL;
42
43 This. Start ();
44}
45}

 

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.