簡體   English   中英

JavaScript計時器無法在頁面重新加載時繼續運行

[英]JavaScript timer does not continue running on page reload

我有下面的腳本代碼,該代碼在Chrome和Firefox上以及在IE中(將網站添加到兼容模式時)都可以正常運行。 由於某種原因,如果尚未在兼容性視圖設置中添加該站點,則每次頁面重新加載時計時器也會這樣做。 為什么會這樣呢? 當我在本地計算機上運行它時,只會在服務器上運行,而不會發生這種情況。

 $(document).ready(function () {
        var intseconds = 0;
        var intminutes = $('#lblTestTime').val();
        //var intminutes = 1;
        //alert(intminutes);
        var inthours = 0;
        var timeString = '';
        var sessionlang = $('#hidElem').text();
        var count = setInterval(function () {



            if ((intseconds == 0) && (intminutes == 0)) {
                intseconds = 0;
                intminutes = 0;
                clearInterval(count);

            }
            else if (intseconds == 0) {
                intseconds = 60;
                intminutes--;
            }

            intseconds--;
            if (intminutes == 0) {
                //inthours--;
                intminutes = 0;
            }
            hours = "0" + inthours + ":";
            if (intminutes < 10) {
                minutes = "0" + intminutes + ":";
            } else {
                minutes = intminutes + ":";
            }

            if (intseconds < 10) {
                seconds = "0" + intseconds + "";
            } else {
                seconds = intseconds + "";
            }
            if (intseconds == -1) {
                // code we require
            }
            else {
                timeString = hours + minutes + seconds;

                if (timeString == "00:05:00") {
                    if ($('#MainContent_hidElem').html() == "1") {
                        alert("You have 5 minutes left to complete the test! Please click OK and continue the test.")
                    }
                    else {
                        alert("¡Le quedan 5 minutos para terminar el test! Haga clic en SÍ y continúe el test")
                    }
                }
                $('#dispClock').html(timeString);
                $("#lblRemainingTime").val(timeString);
                //alert($("#lblRemainingTime").val());
                //intseconds = res[1];
                // intminutes = res[2];

            }
        }, 1000);
    });

如答復中所述,如果重新加載頁面,則Javascript本身不會存儲數據。 重新加載頁面時,請嘗試按CTRL + SHIFT + R強制瀏覽器忽略緩存。

您可以使用cookie來存儲數據或localStorage

暫無
暫無

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

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