标签:判断 speed 环境 网络 break connection var navigator type
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 9 <script> 10 // get the maximum downlink speed 11 // var speed = downlinkmax(); 12 // 13 // // check if it is enough for our amazing feature 14 // if (speed < sufficient) { 15 // disableFeature(); 16 // } 17 18 // (function (window, factory) { 19 // 'use strict'; 20 // if (typeof define === 'function' && define.amd) { 21 // // AMD 22 // define([], factory); 23 // } else if (typeof exports === 'object') { 24 // // Node.js 25 // module.exports = factory(); 26 // } else { 27 // // Browser 28 // window.downlinkmax = factory(); 29 // } 30 // }(this, function factory() { 31 // // public API 32 // return $1; 33 // })); 34 35 // 方法一 36 var limitless = Infinity, 37 nav = navigator, 38 speed, 39 40 connection = nav.connection || nav.mozConnection || nav.webkitConnection || { 41 // 不支持API 42 downlinkMax: limitless 43 }; 44 45 switch (connection.type) { 46 case 'none': 47 speed = 0; 48 break; 49 case '2g': 50 speed = 0.134; 51 break; 52 case 'bluetooth': 53 case 'cellular': 54 speed = 2; 55 break; 56 case '3g': 57 speed = 8.95; 58 break; 59 case '4g': 60 speed = 100; 61 break; 62 case 'ethernet': 63 speed = 550; 64 break; 65 case 'wifi': 66 speed = 600; 67 break; 68 // 其他、未知等. 69 default: 70 speed = limitless; 71 break; 72 } 73 connection.downlinkMax = speed; 74 console.log(connection); 75 76 77 78 79 // var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection; 80 // var type = connection.type; 81 // function updateConnectionStatus() { 82 // alert("Connection type is change from " + type + " to " + connection.type); 83 // } 84 // connection.addEventListener('typechange', updateConnectionStatus); 85 86 87 88 // 方法二 89 var connection = navigator.connection||navigator.mozConnection||navigator.webkitConnection||{tyep:'unknown'}; 90 var type_text = ['unknown','ethernet','wifi','2g','3g','4g','none']; 91 92 alert(connection.type); 93 var re_el = document.getElementById("re"); 94 var btn_el = document.getElementById("btn"); 95 function get_status(){ 96 if(typeof(connection.type) == "number"){ 97 connection.type_text = type_text[connection.type]; 98 }else{ 99 connection.type_text = connection.type; 100 } 101 if(typeof(connection.bandwidth) == "number"){ 102 if(connection.bandwidth > 10){ 103 connection.type = 'wifi'; 104 }else if(connection.bandwidth > 2){ 105 connection.type = '3g'; 106 }else if(connection.bandwidth > 0){ 107 connection.type = '2g'; 108 }else if(connection.bandwidth == 0){ 109 connection.type = 'none'; 110 }else{ 111 connection.type = 'unknown'; 112 } 113 } 114 var html = 'Type : '+connection.type_text; 115 html += '<br>Bandwidth : '+connection.bandwidth; 116 html += '<br>isOnline : '+navigator.onLine; 117 re_el.innerHTML = html; 118 } 119 120 btn_el.onclick = function(){ 121 re_el.innerHTML = 'Waiting...'; 122 get_status(); 123 } 124 125 126 127 128 // get the maximum downlink speed 129 // speed = downlinkmax() 130 131 // check if it is enough for our amazing feature 132 // disableFeature() if speed < sufficient 133 </script> 134 135 </body> 136 </html>
标签:判断,speed,环境,网络,break,connection,var,navigator,type 来源: https://www.cnblogs.com/xiaozhu-zhu/p/11945021.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。