问题描述
我在连接检查方面遇到问题.使用navigator.onLine在我的离子应用程序中进行相同的测试.它在设备之间非常不一致.它是一个跨平台应用程序.
I am facing an issue on connectivity check. Using navigator.onLine to test the same in my ionic app. Its pretty inconsistent across devices. Its a cross platform app.
是否有可能且可靠的替代方法来进行连接检查?
Is there any possible and reliable replacement for connectivity check?
推荐答案
如果浏览器支持navigator.onLine(typeof navigator.onLine ==="boolean"),则连接检查是可靠的.
If the browser supports navigator.onLine (typeof navigator.onLine === "boolean") the connectivity check is reliable.
如果浏览器不支持navigator.onLine(typeof navigator.onLine!=="boolean"),则需要某种黑客手段.
If the browser doesn't support navigator.onLine (typeof navigator.onLine !== "boolean") you need some kind of hack.
一种可能的破解方法是检查在线资源(即图片)的存在
One possible hack is to check for the presence of an online resource (i.e. image)
var imgCheck = new Image();
imgCheck.onerror = function(){ console.log('offline');};
imgCheck.onload = function(){ console.log('online');};
imgCheck.src = <URL_OF_IMAGE> + '?' +new Date().getTime();
这篇关于为什么navigator.onLine不一致?有没有可靠的解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!