码迷,mamicode.com
首页 > Web开发 > 详细

MUI判断网络连接以及监听网络变化JS

时间:2016-08-13 14:05:30      阅读:1570      评论:0      收藏:0      [点我收藏+]

标签:

来源:netChange问题怎么判断网络状态

  • MUI用于获取当前设备的网络类型

 1 function plusReady(){
 2 
 3    var types = {};
 4 
 5    types[plus.networkinfo.CONNECTION_UNKNOW] = "Unknown connection";
 6 
 7    types[plus.networkinfo.CONNECTION_NONE] = "None connection";
 8 
 9    types[plus.networkinfo.CONNECTION_ETHERNET] = "Ethernet connection";
10 
11    types[plus.networkinfo.CONNECTION_WIFI] = "WiFi connection";
12 
13    types[plus.networkinfo.CONNECTION_CELL2G] = "Cellular 2G connection";
14 
15    types[plus.networkinfo.CONNECTION_CELL3G] = "Cellular 3G connection";
16 
17    types[plus.networkinfo.CONNECTION_CELL4G] = "Cellular 4G connection";
18 
19    alert( "Network: " + types[plus.networkinfo.getCurrentType()] );
20 
21 }
22 
23 if(window.plus){
24 
25   plusReady();
26 
27 }else{
28 
29   document.addEventListener("plusready",plusReady,false);
30 
31 }

 

 

  • MUI的netChange事件

 1 mui.plusReady(function() {
 2 document.addEventListener("netchange",onNetChange,false);
 3 function onNetChange(){
 4   //获取当前网络类型
 5   var nt = plus.networkinfo.getCurrentType();
 6   switch(nt){
 7     case plus.networkinfo.CONNECTION_ETHERNET:
 8     case plus.networkinfo.CONNECTION_WIFI:
 9       mui.toast("当前网络为WiFi");
10       break;
11     case plus.networkinfo.CONNECTION_CELL2G:
12     case plus.networkinfo.CONNECTION_CELL3G:
13     case plus.networkinfo.CONNECTION_CELL4G:
14       mui.toast("当前网络非WiFi");
15       break;
16     default:
17       mui.toast("当前没有网络");
18       break;
19   }
20 }

 

MUI判断网络连接以及监听网络变化JS

标签:

原文地址:http://www.cnblogs.com/atomicdog/p/mui_net.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!