
Product
Introducing Tier 1 Reachability: Precision CVE Triage for Enterprise Teams
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
timer-manager-lib
Advanced tools
基于 JS 实现的一个定时任务管理器,可以在 Node 和浏览器环境下使用,你可以轻松地添加、删除、启动、停止和清除定时器,使用同一个 interval 管理全局的 timer,尽可能规避了多个不同的任务复杂性和维护的难度,时间间隔多样性,达到以时间为索引的函数缓存的效果
npm install timer-manager-lib
yarn add timer-manager-lib
pnpm install timer-manager-lib
import { TimerManager } from "timer-manager-lib";
const { TimerManager } = require("timer-manager-lib");
<script src="./node_modules/timer-manager-lib/dist/umd/index.js"></script>
<script>
console.log(TimerManager);
</script>
const timerManager = new TimerManager({
type: "interval", // interval 轮询定时器或 frame 帧定时器
autoStop: true, // 当没有句柄时自动停止定时器
});
使用 add
方法添加定时器。它接受一个回调函数 (handle
) 和一个延迟时间:
const handle = () => {
console.log("定时器触发");
};
const delay = 1000; // 时间以毫秒为单位
const timer = timerManager.add(handle, delay);
使用 delete
方法删除某项定时器,参数提供添加定时器的 timer 对象:
timerManager.delete(timer);
使用 clear
方法停止并清除所有定时器:
timerManager.clear();
使用 startTimer
和 stopTimer
对某个 interval 启动、暂停:
const timerManage = new TimerManager();
const { timers } = timerManage;
const timer1 = timerManage.add(() => {
console.log("hello");
}, 1000);
timerManage.add(() => {
console.log("阿宇的编程之旅");
}, 1000);
const { delay } = timer1;
timerManage.stopTimer(timers[delay]); // 暂停定时器
setTimeout(() => {
timerManage.startTimer(timers[delay]); // 1.5秒后启动定时器
}, 1500);
默认情况下,autoStop
选项设置为 true
。这意味着当没有句柄(timer 的 handlers 为空)时,定时器将自动停止。如果要禁用此行为,请在初始化时将 autoStop
设置为 false
。
// 创建定时器管理器实例
const timerManager = new TimerManager();
// 记录执行步骤
let count = 0;
// 添加定时器项,执行一次后删除
const timer = timerManager.add(() => {
timerManager.delete(timer);
console.log(performance.now(), "del timer", count++);
}, 1000);
// 延时添加定时器项,执行3次后重置
setTimeout(() => {
console.log(performance.now(), "timer2 start");
timerManager.add(() => {
console.log(performance.now(), "timer2", count++);
if (count > 3) {
timerManager.clear();
console.log(performance.now(), "clear");
}
}, 1000);
}, 2000);
timerManager.add(() => {
console.log(performance.now(), "timer3");
}, 500);
https://gitee.com/DieHunter/timer-manager-lib
FAQs
JS实现的一个定时任务管理器
We found that timer-manager-lib demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.