繁体   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