存储DIV的自动化内容,并在页面重新加载中提供



我当前正在创建一个考试门户,该门户需要在所有页面导航上进行一致的计时器,并在时间耗尽后重定向到提交。

目前正在使用本机JavaScript执行时间安排,并在下面显示其时间

//javascript文档

var count = remsecs;
var count2 = remmin;
var count3 = remhrs;
var counter = setInterval(timer, 1000); //1000 will run every 1 second
function timer(){
    count = count - 1;
     if (count3 > 0){
     if (count2 == 0){
        count3 = count3 - 1;
        count2 = 59;
        count = 59;
        timer();
        //clearInterval(counter); //counter ended, do something here 
     //count = count - 1;
     }
     }
     if (count == 0){
        count2 = count2 - 1;
        count = 60;
        timer();
        //clearInterval(counter); //counter ended, do something here 
     //count = count - 1;
     }
document.getElementById("timerhr").innerHTML = '<strong>' + count3 + "</strong>";
document.getElementById("timermin").innerHTML = '<strong>' + count2 + "</strong>";
document.getElementById("timersecs").innerHTML = '<strong>' + count + "</strong>";
var myhrs = document.getElementById("hrs").value = count3;
var mymin = document.getElementById("mins").value = count2;
var mysec = document.getElementById("secs").value = count;
//document.write(count + "<br />" + count2);
if (mymin <= 3 && mymin > -1 && myhrs == 0){
    document.getElementById("timerhr").innerHTML = '<font size="+2" color="red"><strong><blink>' + count3 + "</blink></strong></font>";
    document.getElementById("timermin").innerHTML = '<font size="+2" color="red"><strong><blink>' + count2 + "</blink></strong></font>";
    document.getElementById("timersecs").innerHTML = '<font size="+2" color="red"><strong><blink>' + count + "</blink></strong></font>";
    }
if (mymin == 0 && mysec <= 02 && myhrs == 0){
        window.location = "printresult.php";
        }
}

实际上,我正在尝试避免每次在页面上的任何按钮的按钮上将计时器div的内容存储到数据库中。

是否有一种简单的方法将此计时器存储到浏览器中,并且每次重新加载时都会从中挑选?

谢谢

您是否查找了可能的浏览器存储选项?缺点是它们仅受支持HTML5的浏览器的支持。

并每次从页面上导航时保存count变量。

根据您的情况,您可以使用sessionStorage。您必须像这样更改代码

...
var count = remsecs;
// checks if sessionStorage has an existing examtimer property
if(sessionStorage.hasOwnProperty('examtimer')) { 
  // sessionStorage only stores string to parseInt
  count = parseInt(sessionStorage.examtimer);
}
...
// executes before tab is navigated away or close
window.onbeforeunload = function(){ 
   // saves count into sessionStorage with the key of examtimer
   sessionStorage.setItem("examtimer", count); 
}

@simplex市场如果您需要查看的只是超时:我很确定您根本不需要时间来审议。

  1. 您可以在客户开始课程时获得时间戳。
  2. 使用超时或等效的跟踪剩余时间
  3. 将从当前时间滴答和时间戳中提取

通过使用窗口属性" name" 用于存储,整个会话。

P.S。:

window.name 属性可以处理或超过2.0MB的数据。

最新更新