为数组中的每个间隔 ID 再次运行 set Interval [英] Run set Interval again for every interval ID in array

查看:38
本文介绍了为数组中的每个间隔 ID 再次运行 set Interval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将一堆 setInterval ID 存储在一个数组中.

I am currently storing a bunch of setInterval ID's in an array.

我最初是如何设置间隔的:

How I initially set the intervals:

  intervalId = setInterval(bridgeCall, 10000);
  interValArray.push(intervalId);

我目前有一个按钮,可让我通过调用此函数来停止当前正在运行的所有间隔:

I currently have a button that enables me to stop all intervals currently running by calling this function:

  function stopCampaign() {
    if (interValArray.length > 0) {
      for (i = 0; i < interValArray.length; i++) {
        clearInterval(interValArray[i]);
        console.log("Stopped");
      }
      error = "Stopped"
        Error();
    } else {
      error = "Nothing Running"
        Error();
    }
  }

这会相应地起作用,所以我试图做相反的事情来重新启动它们,如下所示:

And this works accordingly, So I have tried to do the opposite to start them all back up again as seen below:

  function Campaign() {
    if (interValArray.length > 0) {
      for (i = 0; i < interValArray.length; i++) {
        setInterval(interValArray[i],10000);
        console.log("Start");
      }
      error = "Started"
        Error();
    } else {
      error = "No Agents"
        Error();
    }
  }

但不断收到错误消息:

timers.js:275
    timer._repeat();
          ^

TypeError: timer._repeat is not a function
    at wrapper [as _onTimeout] (timers.js:275:11)
    at Timer.listOnTimeout (timers.js:92:15)

是否可以再次设置间隔/启动它们?

Is it possible to setInterval/Start them up again?

推荐答案

除了取消计时器之外,您不能使用计时器句柄做任何事情.这一行:

You can't use the timer handle to do anything other than cancel the timer. This line:

setInterval(interValArray[i],10000);

尝试重启"使用句柄的间隔.这根本不受内置计时器的支持.您需要做与最初启动它所做的相同的事情才能再次启动它.

tries to "restart" the interval using the handle. That simply isn't supported by the built-in timer stuff. You need to do the same thing you did to start it originally to start it again.

您一开始还没有展示如何启动它们,因此我无法建议您将如何重组以使其能够再次启动,但这里有一个综合示例:

You haven't shown how you start them in the first place, so I can't suggest how you would restructure to make it possible to do it again, but here's a synthetic example:

// Initial setup
var timers = [];
for (var n = 0; n < 5; ++n) {
    startTimer(function(val) {
        document.getElementById("a" + val).innerHTML += ".";
    }.bind(null, n), Math.random() * 1000);
}
function startTimer(f, interval) {
    timers.push({
        f: f,
        interval: interval,
        handle: setInterval(f, interval)
    });
}

// Stop them after three seconds
setTimeout(function() {
    console.log("Pausing...");
    timers.forEach(function(timer) {
        clearInterval(timer.handle);
    });
}, 3000);

// Start them two seconds after that
setTimeout(function() {
    console.log("Restarting...");
    timers.forEach(function(timer) {
        timer.handle = setInterval(timer.f, timer.interval);
    });
}, 5000);

// Then stop them for good a couple of seconds later
setTimeout(function() {
    console.log("Stopping");
    timers.forEach(function(timer) {
        clearInterval(timer.handle);
    });
}, 7000);

<div id="a0">&nbsp;</div>
<div id="a1">&nbsp;</div>
<div id="a2">&nbsp;</div>
<div id="a3">&nbsp;</div>
<div id="a4">&nbsp;</div>

这篇关于为数组中的每个间隔 ID 再次运行 set Interval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆