并发并可控制的AjaxObject类

| |
| 不指定 September 23, 2008 @ 10:25, Robin Hoo
之前有讨论过《AJAX并发与控制》,其中给出了一段代码示例。现在将其中的代码修改封装成AjaxObject类作为以后开发的参考使用。该类中需要根据具体应用实际做些修改的两个方法是preworkBeforeRequest/processEcho。

            
01 var max_session=10;
02 var sessions=0;
03 var requestQue=new Array();
04 ////////////////////////////////////////
05 //The prework before Ajax request send//
06 ////////////////////////////////////////
07 ajaxObject.prototype.preworkBeforeRequest=function()
08 {
09     document.getElementById(this.objID).innerHTML="<img align='top' title='"+this.objID+"' xsrc='img/wait.gif' border='0' width='18' height='18'/>";   
10 }
11
12 /////////////////////////////////////////////////
13 //The process after Ajax request echo recieved //
14 /////////////////////////////////////////////////
15 ajaxObject.prototype.processEcho= function()
16 {
17     echoText=this.ajax.responseText;
18     if (echoText.substring(0,2)!="OK")
19     {
20         document.getElementById(this.objID).innerHTML="<img align='top' title='"+echoText+"' xsrc='img/error.gif' border='0' width='18' height='18'/>";
21     }
22     else document.getElementById(this.objID).innerHTML="<img align='top' title='"+echoText+"' xsrc='img/ok.gif' border='0' width='18' height='18'/>";
23 }
24
25 /////////////////////////////////////////////////
26 //The constructor of Ajax object               //
27 /////////////////////////////////////////////////
28 function ajaxObject(_method, _url, _async, _objID)
29 {
30     this.method=_method;
31     this.url=_url;
32     this.async=_async;
33     this.objID=_objID;
34
35     this.ajax = (window.ActiveXObject)?(new ActiveXObject("Microsoft.XMLHTTP")):(window.XMLHttpRequest)?(new XMLHttpRequest()):null;
36    
37     if (this.ajax)
38     {
39         if(sessions<max_session)
40         {
41             sessions++;
42             request.preworkBeforeRequest();
43             this.sendRequest();
44             return;
45         }
46         else
47         {
48             requestQue.push(this);
49         }
50     }
51 }
52
53 /////////////////////////////////////////////////////
54 //The procedure of Ajax object request status check//
55 /////////////////////////////////////////////////////
56 ajaxObject.prototype.processRequest=function(_scope)
57 {
58     if(_scope.ajax.readyState!=4||_scope.ajax.status!=200) return false;
59     _scope.processEcho();
60     sessions--;
61     _scope.checkQue();
62 }
63
64 /////////////////////////////////////////////////////
65 //The procedure of Ajax object send request        //
66 /////////////////////////////////////////////////////
67 ajaxObject.prototype.sendRequest=function()
68 {
69     var scope=this;
70     this.ajax.onreadystatechange = function()
71     {
72         scope.processRequest(scope);
73     };
74     this.ajax.open(this.method, this.url, this.async);
75     this.ajax.send(null);   
76 }
77  
78 /////////////////////////////////////////////////////
79 //The procedure of Ajax object check request queue //
80 /////////////////////////////////////////////////////
81 ajaxObject.prototype.checkQue=function()
82 {
83     if (sessions<max_session && requestQue.length>0)
84     {
85         sessions++;
86         var request = requestQue.shift();
87         if(request)
88         {
89             request.preworkBeforeRequest();
90             request.sendRequest();
91         }
92     }   
93 }
    


最后编辑: Robin Hoo 编辑于2008/09/23 10:42

文章来自: 本站原创
0 comment(s)
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
昵称   密码   游客无需密码
网址   电邮   [注册]
               

验证码 不区分大小写