Why navigator.onLine is inconsistent? Is there any reliable solutions?

岁酱吖の 提交于 2019-12-25 08:34:54

问题


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?


回答1:


If the browser supports navigator.onLine (typeof navigator.onLine === "boolean") the connectivity check is reliable.

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


来源:https://stackoverflow.com/questions/39794405/why-navigator-online-is-inconsistent-is-there-any-reliable-solutions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!