前端定时任务

created:
this.scheduleNightlyTask();

methods:
scheduleNightlyTask() {
      const targetHour = 10; // 目标小时,即晚上1点
      const targetMinute = 57; // 目标分钟,整点
      const targetSecond = 0; // 目标秒,整点

      const now = new Date();
      const delay = this.calculateDelay(targetHour, targetMinute, targetSecond);

      // 如果当前时间已经超过目标时间,将下次执行时间设置为明天的1点
      if (delay < 0) {
        delay += 24 * 60 * 60 * 1000; // 24小时的毫秒数
      }

      setTimeout(function () {
        outLoginClearData();
        // scheduleNightlyTask(); // 递归调用,以确保每天都执行
      }, delay);
    },

    calculateDelay(targetHour, targetMinute, targetSecond) {
      const now = new Date();
      const targetTime = new Date(now);

      targetTime.setHours(targetHour, targetMinute, targetSecond, 0);
      const delay = targetTime - now;

      return delay;
    }

你可能感兴趣的:(前端)