瀏覽代碼

ai生成脚本

修复时间脚本
kindring 2 年之前
父節點
當前提交
37c480c731
共有 3 個文件被更改,包括 166 次插入3 次删除
  1. 0 0
      utils/test.js
  2. 97 0
      utils/time2.js
  3. 69 3
      utils/timer.js

+ 0 - 0
utils/test.js


+ 97 - 0
utils/time2.js

@@ -0,0 +1,97 @@
+class TimeManager {
+  constructor() {
+    this.tasks = new Map(); // Store tasks with their ids
+    this.timer = null; // Main timer
+    this.precision = 100; // Timer precision
+  }
+
+  // Generate a unique id for tasks
+  _generateId() {
+    return Date.now().toString(36) + Math.random().toString(36).substr(2, 5);
+  }
+
+  // Format time in hours, minutes, and seconds
+  _formatTime(ms) {
+    const hours = Math.floor(ms / 3600000);
+    const minutes = Math.floor((ms % 3600000) / 60000);
+    const seconds = Math.floor((ms % 60000) / 1000);
+    return `${hours}小时${minutes}分${seconds}秒`;
+  }
+
+  // Start the main timer
+  _startTimer() {
+    if (!this.timer) {
+      this.timer = setInterval(() => {
+        this._updateTasks();
+      }, this.precision);
+    }
+  }
+
+  // Update tasks based on their type and status
+  _updateTasks() {
+    for (const [id, task] of this.tasks.entries()) {
+      if (task.status === 'running') {
+        if (task.type === 'stopwatch') {
+          task.elapsed += this.precision;
+          task.updateHook(id, this._formatTime(task.elapsed));
+        } else if (task.type === 'countdown') {
+          task.remaining -= this.precision;
+          task.updateHook(id, this._formatTime(task.remaining));
+          if (task.remaining <= 0) {
+            task.endHook(id);
+            this.tasks.delete(id);
+          }
+        }
+      }
+    }
+  }
+
+  // Add a stopwatch task
+  addStopwatch(updateHook) {
+    const id = this._generateId();
+    this.tasks.set(id, {
+      type: 'stopwatch',
+      status: 'running',
+      elapsed: 0,
+      updateHook: updateHook,
+    });
+    this._startTimer();
+    return id;
+  }
+
+  // Add a countdown task
+  addCountdown(duration, updateHook, endHook) {
+      const id = this._generateId();
+      this.tasks.set(id, {
+        type: 'countdown',
+        status: 'running',
+        remaining: duration,
+        updateHook: updateHook,
+        endHook: endHook,
+      });
+      this._startTimer(); // 删除重复的调用
+      return id;
+    }
+
+
+  // 继续任务
+  continueTask(id) {
+    const task = this.tasks.get(id);
+    if (task && task.status === 'paused') {
+      task.status = 'running';
+    }
+  }
+// Pause a task
+  pauseTask(id) {
+    const task = this.tasks.get(id);
+    if (task && task.status === 'running') {
+      task.status = 'paused';
+    }
+  }
+
+  // Remove a task
+  removeTask(id) {
+    this.tasks.delete(id);
+  }
+
+}

+ 69 - 3
utils/timer.js

@@ -5,9 +5,12 @@ class t {
   isPause = false;
   reFreshTime = 10;// 更新数据毫秒
   lastTime = 0;// 上一次的毫秒数
-  constructor(){
+  countdownTime = null; // 倒计时总时间
+  scheduledTasks = []; // 计划的任务
+  constructor(countdownTime){
     this.ms = 0;
     this.timer = null;
+    this.countdownTime = countdownTime;
   }
   init(){
     this.ms = 0;
@@ -43,6 +46,43 @@ class t {
     this.timer = null;
     this.isPause = false;
   }
+  addCountdownTask(duration, callback) {
+    const endTime = new Date().getTime() + duration;
+    this.countdownTasks.push({ endTime, callback });
+  }
+  /** 
+  添加计划任务
+  */
+  addScheduledTask(hour, minute, second, callback, skipDays = 0) {
+    const newTask = { hour, minute, second, callback, skipDays };
+    const index = this.scheduledTasks.findIndex(
+      (task) =>
+        task.hour > newTask.hour ||
+        (task.hour === newTask.hour && task.minute > newTask.minute) ||
+        (task.hour === newTask.hour &&
+          task.minute === newTask.minute &&
+          task.second > newTask.second)
+    );
+    if (index === -1) {
+      this.scheduledTasks.push(newTask);
+    } else {
+      this.scheduledTasks.splice(index, 0, newTask);
+    }
+  }
+  // 修改计划任务的时间
+  updateScheduledTask(index, hour, minute, second) {
+    if (this.scheduledTasks[index]) {
+      this.scheduledTasks[index].hour = hour;
+      this.scheduledTasks[index].minute = minute;
+      this.scheduledTasks[index].second = second;
+    }
+  }
+   // 跳过明天
+  skipTomorrow(index) {
+    if (this.scheduledTasks[index]) {
+      this.scheduledTasks[index].skipDays = 1;
+    }
+  }
   tick(){
     let latencyTime = this.reFreshTime;
     // 获取当前时间,以及上一次时间
@@ -52,10 +92,8 @@ class t {
     // 两次执行间的时间差
     let timeGap = nowTimeStamp - lastTime;
     if((timeGap - this.reFreshTime) < 0){
-    // 时间差小于10秒,需要将多跑的时间补齐回去
     latencyTime = this.reFreshTime + (timeGap - this.reFreshTime) ;
     }else {
-    // 时间差大于10秒,任务执行超时.减去多损耗的时间
     latencyTime = this.reFreshTime - (timeGap - this.reFreshTime) ;
     latencyTime = Math.max(latencyTime,0);
     }
@@ -65,6 +103,34 @@ class t {
     }
     this.ms += timeGap;
     this.lastTime = nowTimeStamp;
+    // 检查计划任务
+    const currentTime = new Date();
+    this.countdownTasks = this.countdownTasks.filter((task) => {
+      if (currentTime >= task.endTime) {
+        task.callback();
+        return false;
+      }
+      return true;
+    });
+    this.scheduledTasks.forEach((task, index) => {
+      if (
+        currentTime.getHours() === task.hour &&
+        currentTime.getMinutes() === task.minute &&
+        currentTime.getSeconds() === task.second
+      ) {
+        if (task.skipDays > 0) {
+          task.skipDays--; // 减少跳过的天数
+        } else {
+          // 1 分钟提醒
+          if (currentTime.getMinutes() === task.minute - 1) {
+            console.log(`任务 ${index} 将在 1 分钟后执行`);
+          }
+          task.callback();
+        }
+      }
+    });
+    
+     
     // 触发hook
     this._reFreshHook(this.ms);
     this.timer = setTimeout(()=>{