JavaScript计时器不会在页面重新加载时继续运行

时间:2015-09-08 13:17:11

标签: javascript jquery html

我有下面的脚本代码,它在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);
    });

1 个答案:

答案 0 :(得分:0)

如回复中所述,如果您重新加载页面,Javascript本身不会存储数据。重新加载页面时,尝试按CTRL + SHIFT + R强制浏览器忽略缓存。

您可以使用Cookie来存储数据或localStorage