簡體   English   中英

防止在頁面刷新時重置計時器

[英]Prevent timer reset on page refresh

我有麻煩了。 我在java腳本中做了倒數計時器代碼但是當我刷新頁面計時器被重置所以如何解決這個問題

這是我的代碼。

 var min = 1; var sec = 59; var timer; var timeon = 0; function ActivateTimer() { if (!timeon) { timeon = 1; Timer(); } } function Timer() { var _time = min + ":" + sec; document.getElementById("Label1").innerHTML = _time; if (_time != "0:0") { if (sec == 0) { min = min - 1; sec = 59; } else { sec = sec - 1; } timer = setTimeout("Timer()", 1000); } else { window.location.href = "page2.html"; } }
 <BODY onload="Timer();"> <div id="Label1"> </div> </BODY>

這種方法使用 localStorage 並且不會在頁面刷新時暫停或重置計時器。

<p id="demo"></p>

<script>
var time = 30; // This is the time allowed
var saved_countdown = localStorage.getItem('saved_countdown');

if(saved_countdown == null) {
    // Set the time we're counting down to using the time allowed
    var new_countdown = new Date().getTime() + (time + 2) * 1000;

    time = new_countdown;
    localStorage.setItem('saved_countdown', new_countdown);
} else {
    time = saved_countdown;
}

// Update the count down every 1 second
var x = setInterval(() => {

    // Get today's date and time
    var now = new Date().getTime();

    // Find the distance between now and the allowed time
    var distance = time - now;

    // Time counter
    var counter = Math.floor((distance % (1000 * 60)) / 1000);

    // Output the result in an element with id="demo"
    document.getElementById("demo").innerHTML = counter + " s";
        
    // If the count down is over, write some text 
    if (counter <= 0) {
        clearInterval(x);
        localStorage.removeItem('saved_countdown');
        document.getElementById("demo").innerHTML = "EXPIRED";
    }
}, 1000);
</script>

Javascript 是客戶端的。 它將在重新加載或任何其他事情時重置。

您的問題的一個簡單解決方案可能是 html5 存儲或會話存儲。

https://www.w3schools.com/html/html5_webstorage.asp

// Store
localStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname");

希望這有幫助!

您正在尋找window.localStorage 這樣的事情應該工作:

<script language="javascript" type="text/javascript">

        var min = 1; 
        var sec = 59;
        var timer;
        var timeon = 0;
        function ActivateTimer() {
            //Don't activate if we've elapsed.
            if(window.localStorage.getItem('elapsed') != null)
                 return;

            if (!timeon) {
                timeon = 1;
                Timer();
            }
        }
        function Timer() {
            var _time = min + ":" + sec;
            document.getElementById("Label1").innerHTML =_time;
            if (_time != "0:0") {
                if (sec == 0) {
                    min = min - 1;
                    sec = 59;
                } else {
                    sec = sec - 1;
                }
                timer = setTimeout("Timer()", 1000);
            }
            else {
                window.localStorage.setItem('elapsed', true);
                window.location.href = "page2.html";
            }
        }
    </script>

暫無
暫無

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

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