最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Tizen tv unintentionally goes to no internet screen - Stack Overflow

programmeradmin5浏览0评论

I am creating a tizen web app where I am checking the internet connection and the do a fetch request to check that the internet is working. but the issue is that it randomly tells me that no internet is available and the API request is not fulfilled. to prevent the request from taking unlimited time I used a setTimeout.


I use the same function in WebOS LG TV and it works perfactly fine. the device I have is Samsung 43in 5 series T5300 HD TV


I am using this function to detect wifi and internet connection. in randomUrl I am using a google, github and other populer urls.

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;
            }
        });
}
发布评论

评论列表(0)

  1. 暂无评论