添加任务队列

This commit is contained in:
李志鹏
2026-01-19 14:25:44 +08:00
parent 87fd5b9a93
commit 0aae85e94d
2 changed files with 117 additions and 86 deletions

View File

@@ -0,0 +1,28 @@
export default class TaskQueue {
constructor() {
this.tasks = [];
this.running = false;
}
// 添加任务
addTask(task) {
this.tasks.push(task);
// 执行任务
this.executeTasks();
}
// 执行任务
async executeTasks() {
if (this.running) {
return;
}
this.running = true;
for (const task of this.tasks) {
await task();
}
this.running = false;
this.clearTasks();
}
// 清空任务队列
clearTasks() {
this.tasks = [];
}
}