简体   繁体   English

一个页面上的多个计时器

[英]Multiple timers on a page

I've been given a task to display multiple timers on a page in a table. 我已经获得了在表格的页面上显示多个计时器的任务。 The start values for these timers are stored in a database and loaded into the view when the page loads. 这些计时器的起始值存储在数据库中,并在页面加载时加载到视图中。

I initially designed this as a single timer. 我最初将其设计为单个计时器。 In that version, using the clearInterval() method to stop the timer from counting down past 0:00 works. 在该版本中,使用clearInterval()方法可以停止计时器从0:00以后开始倒计时。 With the multiple timers, it does not. 使用多个计时器时,不会。

There's no way for me to anticipate how many records are going to display in the table. 我无法预料表中将显示多少条记录。

The single counter variable was how I implemented this when there was only one timer. 单个计数器变量是我只有一个计时器时如何实现的。 That seems to still work to start the countdown process, but doesn't stop it as expected when the clearInterval(counter) is called. 这似乎仍然可以启动倒计时过程,但是在调用clearInterval(counter)时并没有按预期停止它。

var counter;

// NOTE: Does not support days at this time
// Ex: StartTimer(5, 'm', 'timer') for Five Minutes
// Ex: StartTimer(5, 'h', 'timer') for Five Hours
function StartCountdownTimer(timeDistance, timeMeasurement, timerCallback) {

// Add timeDistance in specified measurement to current time
var countDownDate = moment().add(timeDistance, timeMeasurement).toDate();
var timeRemaining;

counter = setInterval(function () {

    // Get Current Time
    var now = new Date().getTime();

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

    let duration = moment.duration(distance * 1000, "milliseconds");

    let hours = duration.hours();
    let minutes = duration.minutes();
    let seconds = duration.seconds();

    if (minutes < 10 && hours && hours > 0) {
        minutes = "0" + minutes;
    }

    if (seconds < 10) {
        seconds = "0" + seconds;
    }

    // If the count down is finished clear the counter interval.
    if (distance < 0) {
        clearInterval(counter);
    }
    else {
        timerCallback(hours, minutes, seconds);
    }
    }, 1000);
}

I would guess that the clearInterval() is not working because there are multiple timers on the page, but I'm not sure of the best way to load multiple variables and assign them to their own setInterval() function to then leverage when doing the clearInterval() later. 我猜想clearInterval()无法正常工作,因为页面上有多个计时器,但是我不确定加载多个变量并将其分配给自己的setInterval()函数以在执行稍后清除clearInterval()。

This is a separate JS file that is called by the HTML in the $(document).ready() function. 这是一个单独的JS文件,由$(document).ready()函数中的HTML调用。

Any ideas on how to get this clearInterval() to work with multiple timers on a page? 关于如何使该clearInterval()与页面上的多个计时器一起使用的任何想法?

Put the various intervals in an object, with a counter variable that increments every time you assign an interval. 将各种间隔放入对象中,并使用一个计数器变量,该变量在您每次分配间隔时都会增加。 Then use the counter as the key and assign its value to the interval. 然后使用计数器作为键并将其值分配给间隔。

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

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