Javascript Network Monitoring and javascript Network Monitoring

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.