import timer, { Timer, TimerInstance, wait } from 'nice-timer';
// 循环
const handle = timer({
time: 1000,
loop: true,
cb(TimerOption) {
app.innerText = Date();
}
});
// 主动关闭循环
handle();
// 循环简写
const handle = timer(
() => {
app.innerText = Date();
},
1000,
true
);
// 不循环
const handle2 = timer({
time: 2000,
loop: false,
cb(TimerOption) {
app.innerText = Date();
}
});
// 不循环的简写
const handle2 = timer(() => {
app.innerText = Date();
}, 2000);
// 循环2次结束
const handle3 = timer({
time: 1000,
loop: 2,
cb(TimerOption) {
app.innerText = Date();
}
});
// 循环2次简写
const handle3 = timer(
() => {
app.innerText = Date();
},
1000,
2
);
// 根据 check 的返回状态执行定时任务(简写不支持check和timerKey)
let a = 0;
const handle4 = timer({
time: 1000,
loop: true,
check(TimerOption) {
a++;
// 仅在a===5或者a===10的时候运行定时任务
if (a === 5 || a === 10) {
return true;
}
return false;
},
cb(TimerOption) {
app.innerText = Date();
}
});
// 更新定时任务
const id = Symbol();
const handle = timer({
time: 1000,
cb() {
// 每隔1秒打印日志1
console.log(1);
},
timerKey: id
});
// 过5秒后更新标识符为id的这个定时任务,让他每隔3秒打印一下日志2
timer(() => {
timer({
time: 3000,
cb() {
console.log(2);
},
timerKey: id
});
}, 5000);
// 自动关闭
handle();
// 直接使用Timer类创建新的实例
const TimerInstance2 = new Timer();
const handle5 = TimerInstance2.create({
time: 1000,
loop: true,
cb(TimerOption) {
app.innerText = Date();
}
});
// 简写
TimerInstance2.create(() => {
app.innerText = Date();
});
// 3秒后暂停 TimerInstance2 实例内所有定时任务
setTimeout(() => {
TimerInstance2.stop();
}, 3000);
// 6秒后重新开启 TimerInstance2 实例内所有定时任务
setTimeout(() => {
TimerInstance2.run();
}, 6000);
// 等待3秒
await wait(3000);
// 或者
wait(3000).then(() => {});
<script src="nice-timer.min.js"></script>
<script>
const app = document.getElementById('app');
const { timer, Timer, TimerInstance } = niceTimer;
const handle = timer({
time: 1000,
loop: true,
cb(TimerOption) {
app.innerText = Date();
}
});
// 主动关闭 handle 定时任务
handle()
// 创建新的实例
const TimerInstance2 = new Timer()
const a2 = TimerInstance2.create({
time: 1000,
loop: true,
cb(TimerOption) {
app.innerText = Date();
}
})
// 停止 TimerInstance2 实例里面的所有定时任务
TimerInstance2.stop()
// 运行 TimerInstance2 实例里面的所有定时任务
TimerInstance2.run()
</script>
import timer, { Timer, TimerInstance } from 'nice-timer';
import { TimerOption, CreateOption, Handler, KeyType } from 'nice-timer/types';
export interface CreateOption {
// 轮询时间
time: number;
// 定时后的回调函数
cb: Handler;
// 循环
loop?: boolean | number;
// 检查函数,返回true则表示该任务运行,否则不允许该cb,返回false时不运行cb但是该total还是会继续叠加
check?(params: TimerOption): boolean;
// 定时任务的标识符(建议使用symbol),相同的 timerKey 将进行替换传递进来的其他参数,定时任务将按照新的参数进行运行
timerKey?: KeyType;
}
export interface TimerOption extends CreateOption {
// 该定时任务的唯一值
id: symbol;
// 该任务的最后一次运行时间
lastTime: number;
// 该任务总运行次数
total: number;
// 该任务cb运行次数
times: number;
// 关闭当前任务函数
targetCloseHandler: () => void;
}
const option: CreateOption = {
time: 1000,
cb(TimerOption) {
console.log(new Date());
}
};
const handle = timer(option);
// 简写
const handle2 = timer(() => {
console.log(new Date());
}, 1000);
const TimerInstance2 = new Timer();
const handle2 = TimerInstance2.create(option);