首页 > 解决方案 > 如何检查 jquery.each 循环中正在运行的计时器

问题描述

我正在使用easytimer(https://github.com/albert-gonzalez/easytimer.js)并且遇到了问题。我在一页上创建了多个计时器,每个计时器都有自己的开始和停止按钮。现在我想确保只有一个计时器同时运行。所以我想如果我点击计时器 3 的开始按钮,它应该检查所有其他计时器是否正在运行然后停止它们。

下面是我的代码

        $('tr.step').each(function (index) {
            var timer = new easytimer.Timer();

            btnStart.off('click').on('click', (event) => {
                event.preventDefault();
                if (timer.isRunning()) {
                    console.log(timer + index + 'is running');
                }
                timer.start({
                    callback: function (timer) {
                        valTimer.html(
                            timer.getTimeValues().toString()
                        );
                    }
                });
                btnStart.hide();
                btnStop.fadeIn(1000);
            });

            btnStop.off('click').on('click', (event) => {
                event.preventDefault();
                timer.stop();
            });

因此,请任何人帮助我如何检查正在运行的计时器并在开始计数新计时器之前将其停止。

标签: javascriptjquerytimer

解决方案


推荐阅读