简体   繁体   English

JavaScript clearInterval() 不清除计时器

[英]JavaScript clearInterval() does not clear the timer

I'm trying to set up a timer that will play a sound after a certain amount of minutes have passed.我正在尝试设置一个计时器,该计时器将在经过一定时间后播放声音。 The number of minutes can be set in the input field time .分钟数可以在输入字段time

The problem is clearInterval function is not working.问题是clearInterval函数不起作用。

When I set a timer to let's say 1 minute and afterward to 2 minutes, both timers are active.当我将计时器设置为 1 分钟,然后设置为 2 分钟时,两个计时器都处于活动状态。

How to remove the first-timer after changing the timer to 2 minutes only the new one will play a sound?如何在将计时器更改为 2 分钟后删除第一个计时器只有新计时器会播放声音?

HTML: HTML:

<input id="time" /> 
<button onclick="timeFunction()">
    set Timer
</button>

<p id ="timer"></p>  

JS: JS:

function timeFunction() {

var minuten = document.getElementById("time").value;
var sec = minuten * 60;
var msec = sec * 1000;

document.getElementById("timer").innerHTML = "Timer set to " + minuten + " minutes";

clearInterval(inter);

var inter = setInterval(function(){
   document.getElementById('gong').play(); }
    ,msec);

}

var inter is a variable that only exists in your timeFunction . var inter是一个只存在于timeFunction中的timeFunction When the function finishes, inter does not exist anymore.当函数完成时, inter不再存在。 When clearInterval runs, inter is undefined, because you haven't assigned it a value yet.clearInterval运行时, inter未定义,因为您尚未为其分配值。

To fix it, declare the inter variable outside of your function.要修复它,请在函数外部声明inter变量。 This will allow it to keep a value between multiple executions of timeFunction .这将允许它在timeFunction多次执行之间保持一个值。

var inter;

function timeFunction() {
  // other code

  clearInterval(inter);

  inter = setInterval(function(){
    document.getElementById('gong').play();
  }, msec);
}

Why don't you return the interval,你为什么不返回间隔,

function timeFunction() {

var minuten = document.getElementById("time").value;
var sec = minuten * 60;
var msec = sec * 1000;

document.getElementById("timer").innerHTML = "Timer set to " + minuten + " minutes";

return setInterval(function(){
   document.getElementById('gong').play(); }
    ,msec);

}

then you could go,那你就可以走了

let intervals = timeFunction();

then you can clear it like so,然后你可以像这样清除它,

clearInterval(intervals);

this way you control how many times it iterates.这样你就可以控制它迭代的次数。

If you want this to run only once clear the interval after your work has been done.如果您希望它只运行一次,请在完成工作后清除间隔。 So your function can be re-written like this:所以你的函数可以像这样重写:

function timeFunction() {
  var minuten = document.getElementById("time").value;
  var sec = minuten * 60;
  var msec = sec * 1000;
  document.getElementById("timer").innerHTML = "Timer set to " + minuten + " minutes";
  var inter = setInterval(function() {
    document.getElementById('gong').play();
    clearInterval(inter);
  }, msec);
}

EDIT编辑

You can also return inter variable from your function so you can use clearInterval whenever your script's logic needs the interval to stop.您还可以从函数中返回inter变量,以便在脚本逻辑需要间隔停止时使用clearInterval

function timeFunction() {
  var minuten = document.getElementById("time").value;
  var sec = minuten * 60;
  var msec = sec * 1000;
  document.getElementById("timer").innerHTML = "Timer set to " + minuten + " minutes";
  var inter = setInterval(function() {
    document.getElementById('gong').play();
  }, msec);
  return inter;
}

let intv = timeFunction();

// rest of your javascript 
// ...

clearInterval(intv);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM