Symbol(asyncId) 是什么,你能用它来清除间隔吗?
What is Symbol(asyncId) and can you use it to clear intervals?
我在 NodeJS 中使用 setInterval()
并看到它返回了一个包含一些信息的超时对象,其中之一是 Symbol(asyncId)
。
什么是 Symbol(asyncId)
,是否可以将其用作 clearInterval()
区间的参考?如果可以,怎么做?
这是我当前的代码:
interval = setInterval(async function () {
prepareObj(message);
console.log(interval);
const intervalID = Reflect.ownKeys(interval).find(
(key) => key.toString() === "Symbol(asyncId)"
);
await saveID(message, interval[intervalID]);
}, 5000);
setInterval()
被 运行 多次,我需要另一种方法来清除每个单独的间隔,因为 interval
将被重新分配以包含最新的 Timeout
对象。我试图将 Symbol(asyncId)
的值存储在 SQLite 数据库中,以便稍后访问该值以清除间隔但没有成功。
尝试使用地图
let intervals = new Map();
interval = setInterval(async function () {
prepareCard(message);
console.log(interval);
await saveID(message, interval);
}, 5000);
async function saveID(message, id) {
intervals.set(`${message.channel.id}`, { interval });
}
// Clearing interval
const objToClear = intervals.get(channel);
console.log(objToClear);
const clear = clearInterval(objToClear["interval"]);
console.log(clear);
调用clearTimeout()
时,我只清除了最近的设置间隔,没有清除目标间隔。
// this will print "hi" to your console every second
const myIntervalId = setInterval(() => console.log("hi"), 1000);
// this will stop the "hi" from being printed
clearInterval(myIntervalId)
有趣的事实... setTimeout
也是 returns 一个 id,并且与 setInterval
共享相同的 id 名称空间,因此您可以从 setTimeout
中清除一个 id clearInterval
!
What is Symbol(asyncId)
这是一个 internal symbol used in the timer implementation of node.js to realise async hooks 定时器。
…and can you use it to clear intervals?
没有。 没有查找 table afaics,即使有你也不应该乱用它。 见下文。
I need to store a value in an SQLite DB, to later access this value to clear the interval.
您可以为此想出自己的 ID:
import * as timers from 'timers';
let counter = 0;
const intervals = new Map();
export function setInterval(callback, delay, ...args) {
const timer = timers.setInterval(callback, delay, ...args);
timeouts.set(++counter, timer);
return counter;
}
export function clearInterval(id) {
const timer = intervals.get(id);
timers.clearInterval(timer);
return intervals.delete(id);
}
但是,我刚刚注意到有一个 recent commit(2020 年 6 月)允许 Timer
实例用作对象中的键,提供了一种将它们强制转换为数字的方法:
const timer = setInterval(…);
const id = Number(timer);
…
clearInterval(id);
不确定何时发布:-)
一般性警告:计时器 ID 不是全局唯一的,而是每个线程和程序执行唯一的。为了将它们存储在外部数据库中,这意味着您必须在应用程序启动时删除所有 ID,否则它们将悬空。理想情况下,对于清除所有计时器并删除数据库对它们的引用的干净关闭,这永远不会发生,但您永远无法确定。为了缓解这个问题,您还可以在存储在数据库中的 ID 前加上一个数据库连接 ID 或存储在数据库中并将在每次启动时递增的其他内容。
我在 NodeJS 中使用 setInterval()
并看到它返回了一个包含一些信息的超时对象,其中之一是 Symbol(asyncId)
。
什么是 Symbol(asyncId)
,是否可以将其用作 clearInterval()
区间的参考?如果可以,怎么做?
这是我当前的代码:
interval = setInterval(async function () {
prepareObj(message);
console.log(interval);
const intervalID = Reflect.ownKeys(interval).find(
(key) => key.toString() === "Symbol(asyncId)"
);
await saveID(message, interval[intervalID]);
}, 5000);
setInterval()
被 运行 多次,我需要另一种方法来清除每个单独的间隔,因为 interval
将被重新分配以包含最新的 Timeout
对象。我试图将 Symbol(asyncId)
的值存储在 SQLite 数据库中,以便稍后访问该值以清除间隔但没有成功。
尝试使用地图
let intervals = new Map();
interval = setInterval(async function () {
prepareCard(message);
console.log(interval);
await saveID(message, interval);
}, 5000);
async function saveID(message, id) {
intervals.set(`${message.channel.id}`, { interval });
}
// Clearing interval
const objToClear = intervals.get(channel);
console.log(objToClear);
const clear = clearInterval(objToClear["interval"]);
console.log(clear);
调用clearTimeout()
时,我只清除了最近的设置间隔,没有清除目标间隔。
// this will print "hi" to your console every second
const myIntervalId = setInterval(() => console.log("hi"), 1000);
// this will stop the "hi" from being printed
clearInterval(myIntervalId)
有趣的事实... setTimeout
也是 returns 一个 id,并且与 setInterval
共享相同的 id 名称空间,因此您可以从 setTimeout
中清除一个 id clearInterval
!
What is
Symbol(asyncId)
这是一个 internal symbol used in the timer implementation of node.js to realise async hooks 定时器。
…and can you use it to clear intervals?
没有。 没有查找 table afaics,即使有你也不应该乱用它。 见下文。
I need to store a value in an SQLite DB, to later access this value to clear the interval.
您可以为此想出自己的 ID:
import * as timers from 'timers';
let counter = 0;
const intervals = new Map();
export function setInterval(callback, delay, ...args) {
const timer = timers.setInterval(callback, delay, ...args);
timeouts.set(++counter, timer);
return counter;
}
export function clearInterval(id) {
const timer = intervals.get(id);
timers.clearInterval(timer);
return intervals.delete(id);
}
但是,我刚刚注意到有一个 recent commit(2020 年 6 月)允许 Timer
实例用作对象中的键,提供了一种将它们强制转换为数字的方法:
const timer = setInterval(…);
const id = Number(timer);
…
clearInterval(id);
不确定何时发布:-)
一般性警告:计时器 ID 不是全局唯一的,而是每个线程和程序执行唯一的。为了将它们存储在外部数据库中,这意味着您必须在应用程序启动时删除所有 ID,否则它们将悬空。理想情况下,对于清除所有计时器并删除数据库对它们的引用的干净关闭,这永远不会发生,但您永远无法确定。为了缓解这个问题,您还可以在存储在数据库中的 ID 前加上一个数据库连接 ID 或存储在数据库中并将在每次启动时递增的其他内容。