-3

I want to create a script using javascript or query to check is internet connection still active on every 1 second ?

So was wrote some codes using PING.JS but I have a problem. My script sends 1 PING request on every 1 second and the server will response within 1 second, but My problem is I want to code if for any reason server's reply will not receive within 60 seconds then it will show me an alert. Also, I want to store data on variable how many PING request was received server's reply successfully? I am providing my script link. Please check and tell me where I was wrong.

https://476606.playcode.io

  • 2
    Please embed your code in the question :-) – Fabian Lauer Dec 05 '19 at 10:37
  • Does this answer your question? [Detect the Internet connection is offline?](https://stackoverflow.com/questions/189430/detect-the-internet-connection-is-offline) – ricky Dec 05 '19 at 10:42

1 Answers1

1

As an alternative, there's an experimental browser API called the "Network Information API".

You can use it like this:

var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
var type = connection.effectiveType;

function updateConnectionStatus() {
  console.log("Connection type changed from " + type + " to " + connection.effectiveType);
  type = connection.effectiveType;
}

connection.addEventListener('change', updateConnectionStatus);

https://developer.mozilla.org/en-US/docs/Web/API/Network_Information_API

It's got reasonably good support on mobile browsers, but not so on desktop browsers. This will show you the currently supported browsers: https://caniuse.com/#feat=netinfo

Alex KeySmith
  • 16,657
  • 11
  • 74
  • 152