使用Javascript重新启动计时器

使用Javascript重新启动计时器,javascript,html,timer,setinterval,examine,Javascript,Html,Timer,Setinterval,Examine,我尝试使用JavaScript为我的测验设置计时器。(setInterval),但如果我提前完成测验并单击开始按钮增益,时间将在我停止测验时开始计数。再次单击“开始”按钮后,如何重新启动时间 <script> var seconds = 40; if (localStorage.getItem("counter")) { if (localStorage.getItem("counter") <= 0) {

我尝试使用JavaScript为我的测验设置计时器。(setInterval),但如果我提前完成测验并单击开始按钮增益,时间将在我停止测验时开始计数。再次单击“开始”按钮后,如何重新启动时间

<script>
    var seconds = 40;
    if (localStorage.getItem("counter")) {
        if (localStorage.getItem("counter") <= 0) {
            var value = seconds;
            alert(value);
        } else {
            var value = localStorage.getItem("counter");
        }
    } else {
        var value = seconds;
    }
    document.getElementById("divCounter").innerHTML = value;

    var counter = function() {
        if (value <= 0) {
            localStorage.setItem("counter", seconds);
            value = seconds;
        } else {
            value = parseInt(value) - 1;
            localStorage.setItem("counter", value);
        }
        document.getElementById("divCounter").innerHTML = value;
    };

    var interval = setInterval(function() { counter(); }, 1000);
</script>

var秒=40;
if(localStorage.getItem(“计数器”)){

如果(localStorage.getItem(“计数器”)根据当前代码设置重置计数器所需的操作
value=seconds
并删除
localStorage
中的当前值。 假设您的HTML中有这样一个按钮:

<button type"button" onclick="resetCounter()">Reset</button>

我尝试使用您的代码,但当我单击“重置”按钮时,我无法转到下一页(.php)这包括我的问题和计时器。您在问题中没有提及或共享任何php代码,原则上,我的代码中没有任何内容阻止您导航到新页面。因此,我建议您在问题中添加更多关于您试图完成的任务的细节。
var resetCounter = () => {
  value = seconds;
  localStorage.removeItem("counter");
};