中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Javascript監(jiān)測(cè)網(wǎng)絡(luò)狀況

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
 
(function(){
var network = function(){
    var monitor = this;
      
    /**
     * @param {Funcation} speedInterval
     */
    var speedInterval = null;
    /**
     * @param {Function} networkInterval
     */
    var networkInterval = null;
    /**
     * @param {Function} reNetworkInterval
     */
    var reNetworkInterval = null;
    var time = 5000;
       
    /**
     * 獲取網(wǎng)絡(luò)連接狀態(tài)
     */
    var getConnectState = function(){
        return navigator.onLine ? 1 : 0;
    }; 
    /**
     * 網(wǎng)絡(luò)中斷
     */
    var disconnect = function(){
        // TODO ... 
        console.log("網(wǎng)速中斷");
        window.clearInterval(reNetworkInterval);
        reNetworkInterval = null;
          
        endSpeed();
        endNetwork();
          
        window.setTimeout(function(){
            reNetworkInterval = window.setInterval(function(){
                if (getConnectState() == 1) {
                    window.clearInterval(reNetworkInterval);
                    reNetworkInterval = null;
                    startSpeed();
                    startNetwork();
                } else {
                    window.clearInterval(reNetworkInterval);
                    reNetworkInterval = null;
                    disconnect();
                }
            }, time);
        }, 2 * time);
    };
  
    /**
     * 網(wǎng)絡(luò)速度
     */
    var speed = {
            /**
             * 網(wǎng)速過(guò)慢
             */
            bad : function(){
                  
                                // TODO ... 
                console.log("網(wǎng)速過(guò)慢");
                  
                window.setTimeout(function(){
                    if(getConnectState() == 1) {
                        window.clearInterval(networkInterval);
                        networkInterval = null;
                        startSpeed();
                    } else {
                        disconnect();
                    }
                }, 2 * time);
            },
            /**
             * 網(wǎng)速中等
             */
            medium : function(){
                                // TODO ... 
                console.log("網(wǎng)速中等");
            },
            /**
             * 網(wǎng)速極佳
             */
            great : function(){
                                // TODO ... 
                console.log("網(wǎng)速極佳");
            }
    };
      
    /**
     * 開(kāi)啟速度監(jiān)測(cè)
     * @private
     */
    var startSpeed = function(){
          
        window.clearInterval(speedInterval);
        speedInterval = null;
        if(getConnectState() == 1) {
            speedInterval = window.setInterval(function(){
                var start = new Date().getTime();
                if (getConnectState() == 1) {
                    var img = document.getElementById("networkSpeedImage");
                    if (!!!img) {
                        img = document.createElement("IMG");
                        img.id = "networkSpeedImage";
                        img.style.display = "none";
                        document.body.appendChild(img);
                    }
                      
                    try {
                        img.src = "http://www.baidu.com/img/baidu_jgylogo3.gif?_t=" + new Date().getTime();
                        img.onload = function(){
                            var end = new Date().getTime();
                            var delta = end - start;
                            if (delta > 200) {
                                speed.bad();
                            } else if (delta > 100) {
                                speed.medium();
                            } else {
                                speed.great();
                            }
                        };
                    } catch(e){
                        speed.bad();
                    }
                } else {
                    // TODO 網(wǎng)絡(luò)斷開(kāi)
                    disconnect();
                }
            }, time);
        }else {
            // TODO 網(wǎng)絡(luò)斷開(kāi)
            disconnect();
        }
    };
      
    /**
     * 停止速度監(jiān)測(cè)
     * @private
     */
    var endSpeed = function(){
        window.clearInterval(speedInterval);
        speedInterval = null;
    };
      
    /**
     * 開(kāi)啟網(wǎng)絡(luò)連接監(jiān)測(cè)
     * @private
     */
    var startNetwork = function(){
        if (getConnectState() == 1) {
            networkInterval = window.setInterval(function(){
                if (getConnectState() == 0) {
                    disconnect();
                }
            }, time);
        } else{
            disconnect();
        }
    };
      
    /**
     * 結(jié)束網(wǎng)絡(luò)連接監(jiān)測(cè)
     * @private 
     */
    var endNetwork = function(){
        window.clearInterval(networkInterval);
        networkInterval = null;
    };
    /**
     * 網(wǎng)絡(luò)監(jiān)控開(kāi)始
     */
    this.start = function(){
        startNetwork();
        startSpeed();
    };
    /**
     * 停止網(wǎng)絡(luò)監(jiān)控
     */
    this.stop = function(){
        endSpeed();
        endNetwork();
    };
};
     window.network = new network();
}).call(this);
  
  
// 調(diào)用的時(shí)候,直接調(diào)用network.start();
 

標(biāo)簽: isp 網(wǎng)絡(luò)

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:生成隨機(jī)密碼的C代碼實(shí)現(xiàn)

下一篇:C++STL之快速排序