HiddenPhox/src/lib/timer.js

36 lines
625 B
JavaScript
Raw Normal View History

2021-05-17 22:35:18 +00:00
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,
};