admin管理员组

文章数量:1402902

I have a dashboard that updates every 5 seconds, and it's displayed on a TV. I implemented a fullscreen toggle button using JavaScript, but every time the page refreshes or the content updates (using location.reload() or fetch()), the page exits fullscreen mode. This is problematic because I need the fullscreen mode to stay active during these automatic updates, without the need for manual intervention.

The main issue is that when the content is updated or the page is refreshed, the fullscreen state is lost, even though the content is updated dynamically.

I tried using localStorage to store the fullscreen state and also used fetch() to update the content dynamically without reloading the page. Here's the code I’m using to toggle fullscreen:

   <script>
        document.getElementById("botao").addEventListener("click", function() {
            if (!document.fullscreenElement) {
                document.documentElement.requestFullscreen();
                localStorage.setItem("fullscreen", "true");
            } else {
                document.exitFullscreen();
                localStorage.setItem("fullscreen", "false");
            }
        });

        // Atualiza a página a cada 5 segundos sem sair do fullscreen
        setTimeout(function() {
            if (localStorage.getItem("fullscreen") === "true") {
                location.reload();
                setTimeout(() => {
                    document.documentElement.requestFullscreen();
                }, 500);
            } else {
                location.reload();
            }
        }, 5000);
    </script>

I expected that after the content was updated or the page refreshed, the fullscreen mode would remain active, preserving the state even during content changes. However, the fullscreen mode is lost after each update or refresh.

本文标签: