当前位置:网站首页>分享一个超好用的 轮询 + 定时器管理器
分享一个超好用的 轮询 + 定时器管理器
2022-07-16 08:01:00 【ivanfor666】
定时器和轮询都是前端小伙伴使用得比较频繁的功能,重复封装、改动都会浪费大量时间,不妨使用下面的一个实战骨架, 再进行自己个性化封装, 实现自己一劳永逸的定时和轮询。
// 定时器管理
class timerManage {
constructor() {}
// 设置定时器, 有timerKey, 则做去重处理
set(timerKey, timerId) {
if (this[timerKey]) {
this.clearTimer(timerKey);
}
this[timerKey] = timerId;
}
// 获取定时器id
get(timerKey) {
return this[timerKey];
}
// 获取当前实例
getAllTimer() {
return this;
}
// 清除定时器
clearTimer(timerKey) {
clearInterval(this[timerKey]);
delete this[timerKey];
}
// 清除所有定时器
clearAllTimer() {
Object.keys(this).forEach((item) => {
clearInterval(this[item]);
delete this[item];
});
}
}
// 创建定时器管理实例
const timerMg = new timerManage();
// 默认执行函数
const default_todoSomeThingFn = () => {
console.log("执行默认函数完毕!");
setTimeout(()=>console.log(Object.keys(timerMg.getAllTimer())))
return true;
};
// 轮询体
const roundQueryFn = (
todoSomeThingFn = default_todoSomeThingFn,
timerKey = ""
) => {
// 创建定时器
let timer = setInterval(todoFn, 100);
// 有传 timerKey
timerKey ? timerMg.set(timerKey, timer) : timerMg.set(timer, timer);
// 执行体
todoFn();
function todoFn() {
console.log(Object.keys(timerMg.getAllTimer()));
// 返回 ture 则 执行内容完毕, 结束轮询
const result = todoSomeThingFn();
if (result) {
timerKey ? timerMg.clearTimer(timerKey) : timerMg.clearTimer(timer);
}
}
};
module.exports = {
roundQueryFn,
};
边栏推荐
- Detailed explanation of Flink resource management
- A lightweight tracking method based on unique identifier of timestamp
- Page break before \ page break inside \ page break after usage
- Jiulian technology development board is officially integrated into the openharmony backbone
- Special testing of mobile applications [reprint ~ Test Engineer full stack technology advancement and practice]
- 澳大利亚规模领先的鞋业公司与Boomi合作以加快电子商务和转型步伐
- AcWing 135. 最大子序和
- Processing TCP and UDP services simultaneously with i/o multiplexing
- [two way PWM drive of Renesas ra6m4 development board]
- A Zuo's aspiration
猜你喜欢
随机推荐
Soft exam (intermediate software designer) exam information
AutoJs学习-应用列表
Win10 timed running program
HMS core graphics and image technology shows the latest functions and application scenarios, and accelerates the construction of digital intelligence life
Detailed explanation of C language "address book"
【批处理DOS-CMD命令-汇总和小结】-符号链接、硬链接、软链接、目录联结(mklink)
Airiot low code development platform, 10 minutes to build the Internet of things system
MANUAL_ Flush is enabled but the buffer is too big solution and cause analysis
Cron expressions are executed from Monday to Friday and only on Saturday and Sunday
This domestic editor will open source soon!
iowait 理解
Build your own website (23)
MANUAL_FLUSH is enabled but the buffer is too big 解决办法 及 产生原因分析
Lscale theme emlog background management panel theme source code
VMware 虚拟机运行卡慢的解决办法
ESRI launches indoor positioning system for facility routing
[batch dos-cmd command - summary and summary] - symbolic link, hard link, soft link, directory link (mklink)
[source code] tensorboard visualizes MNIST recognition training process
全链路压测 :测试要做的准备工作
C language -- the implementation of common string functions





![[C language elementary level] function learning report](/img/06/510f418caa4d69fd5baf7c3ada98c6.png)



