【隨筆】一個簡單的JavaScript非同步處理事件隊列

來源:互聯網
上載者:User

當我們需要連續執行幾個函數,而這幾個函數會一直對頁面元素進行操作時,很可能頁面會出現短暫的卡,然後,一下子爆炸性的出現所有資訊。

一般我們會用setTimeout(fn,0);這種來執行,其實js的setTimeout,特別是IE6的,回應時間最快也要17毫秒左右,所以,設定成0,意義不大。

而且,這種做法,存在不連續,不連貫性。所以,自己寫了個簡單的。

 

 1 /**
 2  * @author floyd
 3  * @name 非同步處理事件隊列
 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      * 添加事件到隊列中
15      * @param {function} fn 函數對象
16      * @param {object} context 內容物件 可為空白
17      * @param {array} arrParam 參數數組 可為空白
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 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.