//思路是根据图片是否加载显示成功
function onLine(callback) {
var img = new Image();
img.src = 'https://www.baidu.com/favicon.ico?_t=' + Date.now();
img.onload = function() {
if (callback) callback(true)
};
img.onerror = function() {
if (callback) callback(false)
};
console.log(img)
}
onLine(function(flag) {
console.log(flag)
if (flag) {
console.log('网络畅通')
} else {
console.log('网络故障')
}
})
// 原文:https://blog.csdn.net/zuggs_/article/details/84553959