-2

大家好,我在我的代码中使用了 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>
4

1 回答 1

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>

我希望这会有所帮助。

于 2013-10-01T20:09:51.847 回答