36 lines
625 B
JavaScript
36 lines
625 B
JavaScript
|
const logger = require("npmlog");
|
||
|
|
||
|
const timerTable = {};
|
||
|
|
||
|
function getTable() {
|
||
|
return timerTable;
|
||
|
}
|
||
|
|
||
|
function add(identifier, callback, delay) {
|
||
|
if (timerTable[identifier] != null) {
|
||
|
clearInterval(timerTable[identifier]);
|
||
|
}
|
||
|
|
||
|
function wrapper() {
|
||
|
try {
|
||
|
callback();
|
||
|
} catch (error) {
|
||
|
logger.error("hf:timer", `Timer "${identifier}" failed: ${error}`);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
timerTable[identifier] = setInterval(wrapper, delay);
|
||
|
}
|
||
|
|
||
|
function remove(identifier) {
|
||
|
if (timerTable[identifier] != null) {
|
||
|
clearInterval(timerTable[identifier]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
getTable,
|
||
|
add,
|
||
|
remove,
|
||
|
};
|