The method of realizing network monitoring by JavaScript _javascript skills

Source: Internet
Author: User
Tags setinterval

This paper illustrates the method of realizing network monitoring by JavaScript. Share to everyone for your reference. The implementation methods are as follows:

The code can monitor whether the network connection and speed of the state.

(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 interrupt/var disconnect = function () {//TODO ... console.log ("internet 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 {window.clearinterval (renetworkinterval);
     Renetworkinterval = null;
    Disconnect ();
  }, time);
 }, 2 * time);
 }; /** * Network Speed * *
 var speed = {/** * speed too slow */bad:function () {//TODO ... console.log ("slow speed");
      Window.settimeout (function () {if (getconnectstate () = 1) {window.clearinterval (networkinterval);
      Networkinterval = null;
     Startspeed ();
     else {disconnect ();
   }, 2 * time);
   /** * Speed Medium/Medium:function () {//TODO ... console.log ("Medium speed");
   /** * speed is very good/great:function () {//TODO ... console.log ("excellent speed");
 }
 };
  /** * Open 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.getElementById ("Networkspeedimage"); if (!!!
      IMG) {img = document.createelement ("img");
      Img.id = "Networkspeedimage"; Img.style.display = "None";
      Document.body.appendChild (IMG);
      try {img.src = "yun_qi_img/baidu_jgylogo3.gif?_t=" + New Date (). GetTime ();
       Img.onload = function () {var end = new Date (). GetTime ();
       var delta = End-start;
       if (Delta >) {Speed.bad ();
       else if (Delta > MB) {speed.medium ();
       else {speed.great ();
     }
      };
     catch (E) {Speed.bad ();
    } else {//TODO network disconnect disconnect ();
  }, time);
  }else {//TODO network disconnect disconnect ();
 }
 };
  /** * Stop Speed monitoring * @private/var endspeed = function () {window.clearinterval (speedinterval);
 Speedinterval = null;
 }; /** * Open Network Connection monitoring * @private/var startnetwork = function () {if (getconnectstate () = = 1) {Networkinterval = WI
    Ndow.setinterval (function () {if (getconnectstate () = 0) {disconnect ();
  }, time);
  } else{disconnect ();
 }
 }; /** * End 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.start () directly when calling.

The

wants this article to help you with your JavaScript programming.

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.