Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.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 - Fatal编程技术网

Javascript定时器暂停&;简历

Javascript定时器暂停&;简历,javascript,Javascript,代码如下 如何添加暂停功能和停止计时器,以及在按resume时设置暂停计时器和resume的按钮 //startTimer将在装载车身时启动计时器 function startTimer() { userInput = "35"; if(userInput.length == 0){ alert("Please enter a value"); } else { var numericExpression = /^[0-9]

代码如下

如何添加暂停功能和停止计时器,以及在按resume时设置暂停计时器和resume的按钮

//startTimer将在装载车身时启动计时器

 function startTimer() {

    userInput = "35";

    if(userInput.length == 0){
        alert("Please enter a value");
    } 
    else {
        var numericExpression = /^[0-9]+$/;
        if(!userInput.match(numericExpression)){
        alert("Please enter a number")
        }       
        else {
        function display( notifier, str ) {
        document.getElementById(notifier).innerHTML = str;
            }

        function toMinuteAndSecond( x ) {
         return Math.floor(x/60) + ":" + x%60;
  }

  function setTimer( remain, actions ) 
  {

    (function countdown() 
        {
       display("countdown", toMinuteAndSecond(remain));         
       actions[remain] && actions[remain]();
       (remain -= 1) >= 0 && setTimeout(arguments.callee, 1000);
        })();
  }


  setTimer(userInput, {
    35: function () { display("score", "6"); document.getElementById("total").value = 6 },
    20: function () { display("score", "4"); document.getElementById("total").value = 4 },
    15: function () { display("score", "3"); document.getElementById("total").value = 3 },
    10: function () { display("score", "2"); display("notifier", "10 seconds left"); document.getElementById("total").value = 2 },
     5: function () { display("score", "1"); display("notifier", "5 seconds left"); document.getElementById("total").value = 1 },
     0: function () { display("score", "0"); display("notifier", "Time is up"); document.getElementById("total").value = 0 },
  }); 
}  
}
}
//setTimer设置倒计时并在页面上显示时间

//setTimer(userInput)在计时器范围内随机显示分数,随着计时器的更改,分数也会更改

在某些限制下,我需要暂停计时器,每当我停止计时器时,计时器将显示正确的分数


谢谢你的帮助

这为您提供了一个基本的、可停止的计时器

HMTL:

编辑自:


教程:

代码在哪里?“代码在下面。”什么都看不到。可能只是google“javascript pause settimeout”的重复。我需要编辑上面给定的代码,有很多与此相关的示例,你能帮我编写代码吗?你到底想做什么?我想你有一个计时开关盒,对吗?给我一个简短的介绍你想做什么,也许我能帮上忙。实际上我做了一个简单的游戏,在这个游戏中,计时器从35秒开始,然后随着时间的减少而减少,我在特定的时间段分配了分数。现在有一个要求是在点击按钮时暂停计时器,就是这样。请忽略分数逻辑,我只需要暂停/停止计时器的功能。简单地说,我可以说如何暂停函数倒计时()好的,我将准备一个html文件并通过电子邮件发送给您。我已经给您发送了一封电子邮件,请检查并回复。
<p>A script on this page starts this clock:</p>
<p id="demo"></p>
<p>Elapsed Seconds:</p>
<p id="secondsPassed"></p>

<button onclick="stopTimer()">Stop time</button>
<button onclick="startTimer()">Start Timer</button>
<button onclick="resumeTimer()">Resume Timer</button>
var myVar = setInterval(start ,1000);
var Seconds = 0;

function startTimer() { 
    myVar = setInterval(start ,1000);
    Seconds = 0;
}

function resumeTimer() {    
    myVar = setInterval(start ,1000);
}

function start() {
    var d = new Date();
    document.getElementById("demo").innerHTML = d.toLocaleTimeString();
    Seconds++;
    document.getElementById("secondsPassed").innerHTML = Seconds;
}

function stopTimer() {
    clearInterval(myVar)
}