当我按下后退按钮或重新加载按钮时,我的计时器正在重新启动。怎么避免这个?

时间:2013-10-01 20:00:01

标签: javascript html forms timer

大家好我在我的代码中使用了20分钟的计时器。(正在工作)

但是,当我按下后退按钮或重新加载按钮时重启。 怎么避免这个? 任何人都有代码禁用浏览器中的后退按钮?

任何人都可以帮助我。

<html>
<head>
<script language="javascript">

function timedText()
{

var x=document.getElementById('txt');
var wc=setTimeout(function(){document.getElementById("txt").disabled=true;},100);
var tu=setTimeout(function(){x.value="20 min "},100);

var flag=1200000;
var to = setInterval(function(){if(flag > 0)
                                {
                                    x.value= flag/60000 + " min";
                                    flag-=60000;
                                }
                                else
                                {
                                    document.getElementById("submit_id").click();
                                    clearInterval(to);
                                }
                               },60000);
}
</script>
</head>
<body onload="timedText()">

<form action="">

<p align='right'><b>Time left </b><input type="text" id="txt" /></p>

</form>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

如果我理解你想要的是一个倒计时秒:

// set the date we're counting down to
var target_date = new Date("Aug 15, 2019").getTime();

// variables for time units
var days, hours, minutes, seconds;

// get tag element
var countdown = document.getElementById("countdown");

// update the tag with id "countdown" every 1 second
setInterval(function () {

    // find the amount of "seconds" between now and target
    var current_date = new Date().getTime();
    var seconds_left = (target_date - current_date) / 1000;

    // do some time calculations
    days = parseInt(seconds_left / 86400);
    seconds_left = seconds_left % 86400;

    hours = parseInt(seconds_left / 3600);
    seconds_left = seconds_left % 3600;

    minutes = parseInt(seconds_left / 60);
    seconds = parseInt(seconds_left % 60);

    // format countdown string + set tag value
    countdown.innerHTML = days + "d, " + hours + "h, "
    + minutes + "m, " + seconds + "s";  

}, 1000);

在html中:

<span id="countdown"></span>

我希望这会有所帮助。