js获得用户网络状况API

js获得用户网络状况API

这是一个实验中的功能,目前还有许多浏览器不兼容此功能某些浏览器尚在开发中

1. 网络类型 effectiveType: 可以得到2g,3g,4g

connectionInfo = navigator.connection返回一个只读对象:

返回网络连接状态NetworkInformation对象,包括.downlink(网络下行速度)  effectiveType(网络类型) onchange(有值代表网络状态变更) rtt(估算的往返时间) saveData(打开/请求数据保护模式)





2. 判断网络
connection.type: 网络状态 API 可以获取到系统的网络连接信息,比如说连接方式是 WiFi 还是蜂窝。


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

function updateConnectionStatus() {
  console.log("设备的网络连接从" + type + "变成了" + connection.type);
}

connection.addEventListener('change', updateConnectionStatus);

作者:dlm17
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/dlm17/p/12517921.html