Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 防止页面刷新时计时器重置_Javascript_Html - Fatal编程技术网

Javascript 防止页面刷新时计时器重置

Javascript 防止页面刷新时计时器重置,javascript,html,Javascript,Html,我有麻烦了。我在java脚本中编写了倒计时代码,但是当我刷新页面时,计时器被重置了,那么如何解决这个问题呢 这是我的密码 var最小值=1; var-sec=59; 无功定时器; var-timeon=0; 功能激活体(){ 如果(!timeon){ timeon=1; 定时器(); } } 函数计时器(){ var_time=min+“:”+秒; document.getElementById(“Label1”).innerHTML=\u time; 如果(_time!=“0:0”){ 如果

我有麻烦了。我在java脚本中编写了倒计时代码,但是当我刷新页面时,计时器被重置了,那么如何解决这个问题呢

这是我的密码

var最小值=1;
var-sec=59;
无功定时器;
var-timeon=0;
功能激活体(){
如果(!timeon){
timeon=1;
定时器();
}
}
函数计时器(){
var_time=min+“:”+秒;
document.getElementById(“Label1”).innerHTML=\u time;
如果(_time!=“0:0”){
如果(秒==0){
min=min-1;
秒=59;
}否则{
秒=秒-1;
}
timer=setTimeout(“timer()”,1000);
}否则{
window.location.href=“page2.html”;
}
}

Javascript是客户端的。它将在重新加载或任何其他事情时重置

解决问题的简单方法可能是html5存储或会话存储


希望这有帮助

您正在查找
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>

var min=1;
var-sec=59;
无功定时器;
var-timeon=0;
功能激活体(){
//如果我们已经过了,请不要激活。
if(window.localStorage.getItem('appeased')!=null)
返回;
如果(!timeon){
timeon=1;
定时器();
}
}
函数计时器(){
var_time=min+“:”+秒;
document.getElementById(“Label1”).innerHTML=\u time;
如果(_time!=“0:0”){
如果(秒==0){
min=min-1;
秒=59;
}否则{
秒=秒-1;
}
timer=setTimeout(“timer()”,1000);
}
否则{
window.localStorage.setItem('appeased',true);
window.location.href=“page2.html”;
}
}

此方法使用本地存储,不会在页面刷新时暂停或重置计时器

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

变量时间=30;//这是允许的时间 var saved_countdown=localStorage.getItem('saved_countdown'); if(保存的\u倒计时==null){ //设置我们倒计时的时间,以使用允许的时间 var new_countdown=new Date().getTime()+(time+2)*1000; 时间=新的倒计时; setItem('saved\u countdown',new\u countdown); }否则{ 时间=保存的\u倒计时; } //每1秒更新一次倒计时 变量x=设置间隔(()=>{ //获取今天的日期和时间 var now=new Date().getTime(); //找出现在和允许时间之间的距离 var距离=时间-现在; //计时器 变量计数器=数学楼层((距离%(1000*60))/1000); //在id=“demo”的元素中输出结果 document.getElementById(“demo”).innerHTML=计数器+“s”; //如果倒计时结束,写一些文字
如果(反向粘贴链接中的重要点,或者这将在将来被标记为仅链接回答,则最好链接到MDN文档,而不是w3schools.com。我觉得w3schools显示的内容更清晰。您可以轻松找到所需的内容。快速旁注
setTimeout(“Timer()”,1000);
应为
setTimeout(计时器,1000);
<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>