• xmlHttp池解决多ajax请求问题


    var XMLHttp = {
        _objPool: [],
        
        _getInstance: 
    function ()
        
    {
            
    for (var i = 0; i < this._objPool.length; i ++)
            
    {
                
    if (this._objPool[i].readyState == 0 || this._objPool[i].readyState == 4)
                
    {
                    
    return this._objPool[i];
                }

            }

            
            
    // IE5中不支持push方法
            this._objPool[this._objPool.length] = this._createObj();

            
    return this._objPool[this._objPool.length - 1];
        }
    ,

        _createObj: 
    function ()
        
    {
            
    if (window.XMLHttpRequest)
            
    {
                
    var objXMLHttp = new XMLHttpRequest();

            }

            
    else
            
    {
                
    var MSXML = ['MSXML2.XMLHTTP.5.0''MSXML2.XMLHTTP.4.0''MSXML2.XMLHTTP.3.0''MSXML2.XMLHTTP''Microsoft.XMLHTTP'];
                
    for(var n = 0; n < MSXML.length; n ++)
                
    {
                    
    try
                    
    {
                        
    var objXMLHttp = new ActiveXObject(MSXML[n]);        
                        
    break;
                    }

                    
    catch(e)
                    
    {
                    }

                }

             }
              
            
            
    // mozilla某些版本没有readyState属性
            if (objXMLHttp.readyState == null)
            
    {
                objXMLHttp.readyState 
    = 0;

                objXMLHttp.addEventListener(
    "load"function ()
                    
    {
                        objXMLHttp.readyState 
    = 4;
                        
                        
    if (typeof objXMLHttp.onreadystatechange == "function")
                        
    {
                            objXMLHttp.onreadystatechange();
                        }

                    }
    ,  false);
            }


            
    return objXMLHttp;
        }
    ,
        
        
    // 发送请求(方法[post,get], 地址, 数据, 回调函数)
        sendReq: function (method, url, data, callback)
        
    {
            
    var objXMLHttp = this._getInstance();

            
    with(objXMLHttp)
            
    {
                
    try
                
    {
                    
    // 加随机数防止缓存
                    if (url.indexOf("?"> 0)
                    
    {
                        url 
    += "&randnum=" + Math.random();
                    }

                    
    else
                    
    {
                        url 
    += "?randnum=" + Math.random();
                    }


                    open(method, url, 
    true);
                    
                    
    // 设定请求编码方式
                    setRequestHeader('Content-Type''application/x-www-form-urlencoded; charset=UTF-8');
                    send(data);
                    onreadystatechange 
    = function ()
                    
    {                   
                        
    if (objXMLHttp.readyState == 4 && (objXMLHttp.status == 200 || objXMLHttp.status == 304))
                        
    {
                            callback(objXMLHttp);
                        }

                    }

                }

                
    catch(e)
                
    {
                    alert(e);
                }

            }

        }

    }
    ;

    通过建立xmlHttp池来有效避免多个ajax同时请求冲突的问题,可以复用空闲的xmlHttp对象避免为每个ajax请求生成新xmlHttp对象的问题

    对以上代码可以简化一下,缩短长度

     1
    var XMLHttp = {
     2    _objPool: [],
     3    _getInstance: function ()
     4    {
     5        for (var i = 0; i < this._objPool.length; i ++)
     6        {
     7            if (this._objPool[i].readyState == 0 || this._objPool[i].readyState == 4)
     8            {
     9                return this._objPool[i];
    10            }

    11        }

    12        this._objPool[this._objPool.length] = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
    13        return this._objPool[this._objPool.length - 1];
    14    }
    ,
    15    sendReq: function (method, url, data, callback)
    16    {
    17        var objXMLHttp = this._getInstance();
    18        with(objXMLHttp)
    19        {
    20            url += ((url.indexOf("?")>0? "&" : "?"+ "r="+Math.random();
    21            open(method, url, true);
    22            send(data);
    23            onreadystatechange = function ()
    24            {                   
    25                if (objXMLHttp.readyState == 4 && objXMLHttp.status == 200)
    26                    callback(objXMLHttp);
    27            }

    28        }

    29    }

    30}
    ;
  • 相关阅读:
    项目01-nginx模块
    Spark机器学习
    项目01-手机端模块
    Spark内存管理
    Spark Streaming
    Spark SQL
    Spark Job调度
    Spark master节点HA配置
    机器学习
    07、Spark集群的进程管理
  • 原文地址:https://www.cnblogs.com/eoiioe/p/1418617.html
Copyright © 2020-2023  润新知