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);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM