如何有效地运行多个setIntervals



我正在创建一个机器人,每隔24小时我从数据库中检索一个类别,对于每个类别,我将发送一篇帖子,而每个帖子间隔1小时。(24小时后发送第一封邮件,一小时后发送下一封,等等(

我的问题是,在第二天仍然可以停止某个类别的同时,我该如何设置它。我知道setIntervals有一个id,我可以把它们添加到一个数组中,我可以用这个id停止间隔。然而,我觉得这不是最理想的解决方案。

这是我目前的解决方案,但我觉得使用node.js包或其他一些巧妙的工作可以做得更好。

let intervalIds = [];
let interval = [];
const categories = ["sad", "toebeans", "meow"]
console.log(`${interval.length}`);
categories.forEach(category => {
let id = interval.push(setInterval(() => {console.log(`Interval for ${category}`)}, 6000000000));
intervalIds.push(`${category}=${id}`)
});
// clearInterval(interval[2]) STOPS 6
console.log(interval);
console.log(intervalIds);
let a = 0;
for (let i = 0; i < intervalIds.length; i++) {
let split = intervalIds[i].split("=");
if (split[0] === "sad") {
clear(a);
break;
}
a++;
}
function clear(a) {
clearInterval(interval[a]);
console.log(interval);
}

我希望能够调用一个启动所有定时器的命令,以及另一个允许打开或关闭单个类别的命令

提前感谢大家的帮助。

这样的东西怎么样?

const myThings = ['thing1', 'thing2', 'thing3'];
function runThings(things, msBetweenThings) {
let nextThingIndex = 0;
console.log('running things');
const myInterval = setInterval(() => {
if (nextThingIndex >= 0) {
console.log(things[nextThingIndex++]);
if (nextThingIndex >= myThings.length) {
console.log('ran out of things!')
clearInterval(myInterval);  // cleanup when done
}
}
}, msBetweenThings);
}
// start it up by sending a list of things and a delay
runThings(myThings, 10000)

更好的解决方案是制作一个区间,并在这个区间中迭代所有类别。然后您可以将类别"设置"为活动或非活动:

let categories = new Map();
categories.set("sad", true);
categories.set("toebeans", true);
categories.set("moew", true);
setInterval(() => {
Array.from(categories.keys()).forEach((category) => {
if (categories.get(category)) console.log(`Interval for ${category}`);
//console.log(categories);
});
}, 6000000000);
function setActive(category, isActive) {
categories.set(category, isActive);
}
//disable some category
setActive("sad", false);

创建一个javascript类,通过发送函数(命令(、字符串(类别(和间隔来注册命令的类别。

myClass.register("some-category", 60000, function(){ doWork(); }); 

该类需要在一个单独的数组中维护每个类别。我会制作一个数组对象,因为它们可以通过字符串引用。例如

categories["some-category"].push(command);

现在您有了一个注册工具和一种存储排序命令的方法。在javascript类中添加一些方法来启动和停止这些:

startAll();
stopAll();
startCategory("some-category");
stopCategory("some-category");

以下是注册方法的示例:

function register(category, interval, callback) {
if(!this.categories[category]) this.categories[category] = [];
let token = setInterval(callback, interval);
this.categories[category].push({token:token, callback:callback});
}

以下是stopCategory方法的示例:

function stopCategory(category) {
if(this.categories[category]) {
for(let i=0; i<this.categories[category].length; i++) {
clearInterval(this.categories[category][i].token);
}
}
}
let curPost = 0;
const CategoryTimer = setInterval(() => {
// Database Call
let categories = ["sad", "toebeans", "meows", "tucked", "smol", "chonky"];

// Make post
console.log(`Post for ${categories[curPost]}`);
if (curPost === categories.length - 1) {
curPost = -1;
console.log("Restart!");
}
curPost++;
}, 1000000;

最新更新