js获得用户网络状况API

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

js获得用户网络状况API

对味Ming   2020-03-18 我要评论

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

 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们