简体   繁体   English

如何在JavaScript / Jquery中重置计时器?

[英]How do i reset the timer in javascript/Jquery?

I have a timer that begins when my application loads. 我有一个计时器,该计时器在我的应用程序加载时开始。 But i want to reset my timer on click of button. 但是我想在单击按钮时重置计时器。 But when i try to reset the timer weird values starts getting displayed in timer section. 但是,当我尝试重置计时器时,奇怪的值开始在计时器部分显示。

Please find my code below: 请在下面找到我的代码:

HTML: HTML:

    <font size="4"><span class="myClass" id="time">02:00</span></font>   
<button onclick="myFunction()">Click me</button> 

Javascript: Javascript:

$( document ).ready(function() 
            {
                var minute = 60 * 2,
                display = document.querySelector('#time');
                begin(minute, display);

            });


function myFunction()
{
var minute = 60 * 2,
display = document.querySelector('#time');
begin(minute, display);

}

    function begin(duration, display) {
                var timer = duration, minutes, seconds;
                setInterval(function () {
                    minutes = parseInt(timer / 60, 10)
                    seconds = parseInt(timer % 60, 10);

                    minutes = minutes < 10 ? "0" + minutes : minutes;
                    seconds = seconds < 10 ? "0" + seconds : seconds;

                    display.textContent = minutes + ":" + seconds;

                    if (--timer < 0) 
                    {
                        timer = duration;
                    }
                }, 1000);
            }

I think you have multiple intervals running at the same time so you need to clean them up using clearInterval(interval); 我认为您同时运行多个时间间隔,因此需要使用clearInterval(interval);清理它们。

$( document ).ready(function() 
        {
            var minute = 60 * 2,
            display = document.querySelector('#time');
            begin(minute, display);

        });
var interval;

function myFunction()
{
var minute = 60 * 2,
display = document.querySelector('#time');
clearInterval(interval);
begin(minute, display);
}

function begin(duration, display) {
            var timer = duration, minutes, seconds;
            interval = setInterval(function () {
                minutes = parseInt(timer / 60, 10)
                seconds = parseInt(timer % 60, 10);

                minutes = minutes < 10 ? "0" + minutes : minutes;
                seconds = seconds < 10 ? "0" + seconds : seconds;

                display.textContent = minutes + ":" + seconds;

                if (--timer < 0) 
                {
                    timer = duration;
                }
            }, 1000);
        }

something like this. 这样的事情。

In your my function reset the timer value using innerHTML or innerTEXT 在我的函数中,使用innerHTML或innerTEXT重置计时器值

function myFunction()
{
  var minute = 60 * 2,
  display = document.querySelector('#time');
  display.innerHTML = '02:00';
  begin(minute, display);

}

Try to reference your Interval with a variable. 尝试使用变量引用您的时间间隔。

var myInterval = setInterval(function(){
....
....
....
},1000);


// Button Click ->
clearInterval(myInterval);

You need to stop the setInterval. 您需要停止setInterval。 So make a global variable var refreshIntervalId = null . 因此,使全局变量var refreshIntervalId = null Inside begin() function, insert this line at the top begin()函数中,将此行插入顶部

if (refreshIntervalId) {
    clearInterval(refreshIntervalId);
}

and call setInterval like this 然后像这样调用setInterval

refreshIntervalId = setInterval(function () { ..

That is good enough. 够了。

Javascript setInterval() method returns an interval Id which can be used to terminate/stop the interval. Javascript setInterval()方法返回一个间隔ID,可用于终止/停止该间隔。 Using clearInterval() function the the loop of method execution can be stopped. 使用clearInterval()函数可以停止方法执行的循环。

In you case just store the interval Id and on click event function add the clearInterval(intervalId) function and pass the interval Id. 在您的情况下,只需存储间隔ID,然后在单击事件函数中添加clearInterval(intervalId)函数并传递间隔ID。

var intervalId = null;
    myFunction(){
         if(intervalId != null){
          clearInterval(intervalId);
         }
    var minute = 60 * 2;
        display = document.querySelector('#time');
        begin(minute, display);
    }

Store the interval Id 存储间隔ID

intervaluId = setInterval(function(){ ....
                          },1000)

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

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