簡體   English   中英

javascript僅在選中復選框時才重新加載html頁面

[英]javascript reload html page only when checkbox is checked

如果選中了復選框,我想每x秒重新加載一次頁面。
我發現一個代碼被重裝,並添加了一個if語句。
但這是行不通的。 該腳本僅在沒有if statemant的情況下起作用。
我想念什么?

<!-- Reload page-->
setTimeout(function()
        {
            if (document.getElementById('CheckBoxReloadPage').checked)
            {
                location.reload(true);
                document.getElementById("CheckBoxReloadPage").checked = true;
            }
        },
        5000);

謝謝
帕特里克

您的網頁5秒后重新加載正確的,但只有當你選中該復選框前5秒鍾內你的頁面加載后。

如果希望檢查從現在開始 每5秒而不是5秒進行 一次,請將setTimeout更改為setInterval

然后,您可能會想到,當頁面重新加載時,所有內容都會恢復到原來的樣子-包括復選框。 您必須將復選框的狀態存儲在例如會話存儲中,並在加載時將其設置為復選框:

// Gets the reference of the checkbox once and for all
var myCheckbox = document.getElementById('CheckBoxReloadPage');

// Sets the initial value of the checkbox to the stored state
myCheckbox.checked = sessionStorage.getItem('checkbox-state');

// Listen to every change on the checkbox
myCheckbox.addEventListener('change', function (event) {
    // Store the state of the checkbox to the session storage
    sessionStorage.setItem('checkbox-state', event.target.checked);
});

setInterval(function() {
    if (myCheckbox.checked) {
        location.reload(true);
    }
}, 5000);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM