三星Tizen TV(T5300型号)Web应用随机触发无网络提示问题求助
三星Tizen TV(T5300型号)Web应用随机触发无网络提示问题求助
我正在开发一款Tizen Web应用,需要检测网络连接状态,之后发起fetch请求来确认网络是否真的可用。但现在遇到了一个头疼的问题——它会随机判定“无网络可用”,导致API请求无法正常完成。为了防止请求无限挂起,我给fetch加了3秒的超时处理。
有意思的是,同样的函数在WebOS系统的LG电视上运行完全正常,唯独在我的三星43英寸5系列T5300高清电视上出现这个随机问题。
我用下面这个函数来检测WiFi和网络连接,randomUrls里存的是google.com、github.com这类热门网站的地址,每次随机选一个来请求:
let failureCount = 0; function checkInternetConnection() { // first check if the wifi is connected const isOnline = navigator.onLine; if (!isOnline) { console.log("Internet check failed:", "no wifi"); isPopupShown = true; return; } const randomUrl = randomUrls[Math.floor(Math.random() * randomUrls.length)]; // Create a promise that rejects after 3 seconds to simulate a timeout. const timeoutPromise = new Promise((_, reject) => setTimeout(() => reject(new Error("Timeout error")), 3000) ); // Use Promise.race to either get the fetch result or timeout. Promise.race([ fetch(randomUrl, { method: "GET", cache: "no-cache" }), timeoutPromise ]) .then(() => { console.log("Internet is available"); failureCount = 0; if (isPopupShown) { isPopupShown = false; } }) .catch((error) => { console.log("Internet check failed:", error.message); failureCount = failureCount + 1; console.log("Failure count:", failureCount); // Only show the popup after 3 consecutive failures. if (failureCount >= 3 && !isPopupShown) { isPopupShown = true; } }); }
备注:内容来源于stack exchange,提问作者Muhammad Aamir Khan
相关产品推荐
相关产品推荐

