Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.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_Time_Timer - Fatal编程技术网

可以在不重叠的情况下重置的计时器?-Javascript

可以在不重叠的情况下重置的计时器?-Javascript,javascript,html,time,timer,Javascript,Html,Time,Timer,我是一个彻头彻尾的傻瓜,第一次尝试将它用于学校项目 我的目标是有一个定时器,按下按钮后倒计时。然后使用Unix时间10分钟作为参数调用countdownClock函数。我的代码似乎工作得不错,但再次按下按钮时,它会产生一个奇怪的交替故障,两个计时器同时工作 按下按钮后,我可以做些什么来忘记上一个计时器?谢谢大家! function countdownClock(time){ // Set the date we're counting down to var countDownDate

我是一个彻头彻尾的傻瓜,第一次尝试将它用于学校项目

我的目标是有一个定时器,按下按钮后倒计时。然后使用Unix时间10分钟作为参数调用countdownClock函数。我的代码似乎工作得不错,但再次按下按钮时,它会产生一个奇怪的交替故障,两个计时器同时工作

按下按钮后,我可以做些什么来忘记上一个计时器?谢谢大家!


function countdownClock(time){
  // Set the date we're counting down to
  var countDownDate = (time + "000");
  // Update the count down every 1 second
  var x = setInterval(function() {
    // Get today's date and time
    var now = new Date().getTime();

    // Find the distance between now and the count down date
    var distance = countDownDate - now;

    // Time calculations for days, hours, minutes and seconds
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);

    // Output the result in an element with id="demo"
    document.getElementById("timer").innerHTML = minutes + "m " + seconds + "s ";
    // If the count down is over, write some text
    if (distance < 0) {
      clearInterval(x);
      document.getElementById("timer").innerHTML = "GAME OVER";
    }
  }, 1000);
}

功能倒计时时钟(时间){
//确定我们倒计时的日期
var倒计时日期=(时间+000“);
//每1秒更新一次倒计时
var x=setInterval(函数(){
//获取今天的日期和时间
var now=new Date().getTime();
//找出现在和倒计时日期之间的距离
var距离=倒计时日期-现在;
//天、小时、分钟和秒的时间计算
var分钟=数学楼层((距离%(1000*60*60))/(1000*60));
var秒=数学楼层((距离%(1000*60))/1000);
//在id=“demo”的元素中输出结果
document.getElementById(“计时器”).innerHTML=minutes+“m”+seconds+“s”;
//如果倒计时结束,写一些文字
如果(距离<0){
净间隔(x);
document.getElementById(“计时器”).innerHTML=“游戏结束”;
}
}, 1000);
}

我猜您是从某个地方复制的,因为它已经有了您所需要的-
clearInterval
将为您提供所需的结果,您只需要进行最小的更改

var intervalId;
function countdownClock(time){
  // Set the date we're counting down to
  var countDownDate = (time + "000");
  // clear the old one, if relevant
  if (intervalId)
    clearInterval(intervalId)
  intervalId = setInterval(function() {
    // Get today's date and time
    var now = new Date().getTime();

    // Find the distance between now and the count down date
    var distance = countDownDate - now;

    // Time calculations for days, hours, minutes and seconds
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);

    // Output the result in an element with id="demo"
    document.getElementById("timer").innerHTML = minutes + "m " + seconds + "s ";
    // If the count down is over, write some text
    if (distance < 0) {
      clearInterval(intervalId);
      document.getElementById("timer").innerHTML = "GAME OVER";
    }
  }, 1000);
}
var有效期;
功能倒计时时钟(时间){
//确定我们倒计时的日期
var倒计时日期=(时间+000“);
//如果相关,请清除旧的
如果(有效期内)
clearInterval(有效期间)
intervalId=setInterval(函数(){
//获取今天的日期和时间
var now=new Date().getTime();
//找出现在和倒计时日期之间的距离
var距离=倒计时日期-现在;
//天、小时、分钟和秒的时间计算
var分钟=数学楼层((距离%(1000*60*60))/(1000*60));
var秒=数学楼层((距离%(1000*60))/1000);
//在id=“demo”的元素中输出结果
document.getElementById(“计时器”).innerHTML=minutes+“m”+seconds+“s”;
//如果倒计时结束,写一些文字
如果(距离<0){
clearInterval(intervalId);
document.getElementById(“计时器”).innerHTML=“游戏结束”;
}
}, 1000);
}

添加您的html代码,而不是我们试图猜测您的html代码是什么……您能否为此创建一个新的解决方案,我发现很难理解您在做什么。或者,也可以添加html,这是因为每次单击按钮时,都会创建一个新作业,而上一个间隔作业会继续工作。