Javascript Network Monitoring and javascript Network Monitoring
This article describes how to implement network monitoring using Javascript. Share it with you for your reference. The specific implementation method is as follows:
This code can monitor whether the network is connected and the speed of the network.
(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; /*** get network connection status */var getConnectState = function () {return navigator. onLine? 1: 0;};/*** network interruption */var disconnect = function () {// TODO... console. log ("network speed interruption"); 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 {wind Ow. clearInterval (reNetworkInterval); reNetworkInterval = null; disconnect () ;}, time) ;}, 2 * time );}; /*** network speed */var speed = {/*** network speed too slow */bad: function () {// TODO... console. log ("network speed too slow"); window. setTimeout (function () {if (getConnectState () = 1) {window. clearInterval (networkInterval); networkInterval = null; startSpeed () ;}else {disconnect () ;}, 2 * time) ;},/*** moderate network speed */medium: Function () {// TODO... console. log ("Medium network speed") ;},/*** excellent network speed */great: function () {// TODO... console. log ("excellent network speed") ;}};/*** enable Speed monitoring * @ 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. getEle MentById ("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 network disconnection disconnect () ;}}, time) ;}else {// TODO network disconnection disconnect ();}}; /*** stop speed monitoring ** @ private */var endSpeed = function () {window. clearInterval (speedInterval); speedInterval = null;};/*** enable network connection monitoring * @ private */var startNetwork = function () {if (getConnectState () = 1) {networkInterval = window. setInterval (function () {if (getConnectState () = 0) {disconnect () ;}, time) ;}else {disconnect ();}}; /*** stop network connection monitoring ** @ private */var endNetwork = function () {window. clearInterval (networkInterval); networkInterval = null;};/*** Network Monitoring start */this. start = function () {startNetwork (); startSpeed () ;};/***** stop network monitoring */this. stop = function () {endSpeed (); endNetwork () ;}}; window. network = new network ();}). call (this); // call network directly. start ();
I hope this article will help you design javascript programs.