HiddenPhox/src/lib/timer.js
Cynthia Foxwell a259ee8da2 write own logger
since npmlog wants to add in "node-pre-gyp" onto every line
2021-08-22 14:20:49 -06:00

35 lines
630 B
JavaScript

const logger = require("./logger.js");
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,
};