jquery 基于时间的数值轮换

xmjla07d  于 2023-06-22  发布在  jQuery
关注(0)|答案(1)|浏览(95)

我有一个脚本,它应该通过一个常数的值旋转(const旋转)。它应该从某个值和开始日期开始连续运行(const known
列表中的下一个值应始终在30分钟后出现,但有一个例外:如果值的“id”是“193234”(Eldoren),则下一个值应该总是在60分钟(而不是30分钟)之后出现。
问题是,这个异常只对id“193234”的第一次出现有效,而不适用于后面的:

输出:

01:00 Skag
01:30 Pfliep
02:00 Magmaton
02:30 Eldoren // 1 hour until the next value, which is correct
03:30 Skag
04:00 Pfliep
04:30 Magmaton
05:00 Eldoren // should be 1 hour until the next value, too
05:30 Skag // so start should be at 06:00

这是我的脚本和JSFiddle

$(function () {
          if ($("[id^=test]").length) {
            function addHours(date, hours) {
              var copy = new Date(date);
              copy.setTime(copy.getTime() + hours * 60 * 60 * 1000);
              return copy;
            }
    
            function addMinutes(date, minutes) {
              return new Date(date.getTime() + minutes * 60000);
            }
    
            function next(value) {
              return {
                index: (value.index + 1) % rotation.length,
                date: addHours(value.date, 0.5),
              };
            }
    
            const known = { id: 193210, ts: "2023-06-20T17:30:00" };
            const rotation = [
              { id: 193210, name: "Pfliep " },
              { id: 186827, name: "Magmaton" },
              { id: 193234, name: "Eldoren" },
              { id: 193149, name: "Skag" },
            ];
    
            const now33 = new Date();
            const now2 = addHours(now33, 10);
            const now3 = addHours(now33, -0);
            const end = addHours(now33, rotation.length * 2);
    
            var cur = {
              index: rotation.findIndex((li) => li.id == known.id),
              date: Date.parse(known.ts),
            };
    
            while (cur.date < now3) {
              cur = next(cur);
            }
    
            const out = document.getElementById("test");
    
            let increaseMinutes = 0;
    
            while (cur.date < end && cur.date < now2 && cur.date > now3) {
              const li = document.createElement("li");
              li.className = "test";
    
              var link = document.createElement("a");
              link.textContent = `${rotation[cur.index].name}`;
              link.href = "dadada";
              link.class = "dadada";
    
              li.innerText = `${rotation[cur.index].name} (${cur.date.toLocaleString()})`;
    
              var id = `${rotation[cur.index].id}`;
              var title2 = `${rotation[cur.index].name}`;
              var link = "https://de.wowhead.com/npc=";
              var ClassName = "weeklytext";
              var ClassName2 = "weeklytext2";
              var ClassName3 = "peinigertime";
    
              var options = { hour: "numeric", minute: "numeric" };
    
              var date33 = `${addMinutes(cur.date, increaseMinutes).toLocaleString("en-GB", options)}`;
    
              if (id == 193234) {
                increaseMinutes = 30;
              } else if (increaseMinutes > 0) {
                increaseMinutes += 0;
              }
    
              $(
                '<li class="' +
                  ClassName2 +
                  '"><div class="' +
                  ClassName3 +
                  '"> ' +
                  date33 +
                  '</div> <a class="' +
                  ClassName +
                  '" href="' +
                  link +
                  +id +
                  '">' +
                  title2 +
                  "</a></li>"
              ).appendTo(out);
              cur = next(cur);
            }
          }
        });
l2osamch

l2osamch1#

在您的脚本中,“Eldoren”ID的异常处理似乎存在问题。问题在于更新increaseMinutes变量的逻辑。
1.脚本将increaseMinutes初始化为0。
1.当第一次遇到“Eldoren”时,条件id == 193234为true,并且increaseMinutes被设置为30。
1.对于后续值,条件increaseMinutes > 0始终为false,因为increaseMinutes保持在30并且从不更新。
这会导致脚本总是在时间戳上添加30分钟,即使对于“Eldoren”的后续出现也是如此,而不是添加60分钟的预期行为。

$(function () {
  if ($("[id^=test]").length) {
    function addHours(date, hours) {
      var copy = new Date(date);
      copy.setTime(copy.getTime() + hours * 60 * 60 * 1000);
      return copy;
    }

    function addMinutes(date, minutes) {
      return new Date(date.getTime() + minutes * 60000);
    }

    function next(value) {
      return {
        index: (value.index + 1) % rotation.length,
        date: addMinutes(value.date, value.nextDelay),
        nextDelay: rotation[(value.index + 1) % rotation.length].id === 193234 ? 60 : 30,
      };
    }

    const known = { id: 193210, ts: "2023-06-20T17:30:00" };
    const rotation = [
      { id: 193210, name: "Pfliep " },
      { id: 186827, name: "Magmaton" },
      { id: 193234, name: "Eldoren" },
      { id: 193149, name: "Skag" },
    ];

    const now33 = new Date(); // just to Eldoren be the first, change back to new Date();
    const now2 = addHours(now33, 10);
    const now3 = addHours(now33, -0);
    const end = addHours(now33, rotation.length * 2);

    var cur = {
      index: rotation.findIndex((li) => li.id == known.id),
      date: new Date(known.ts),
      nextDelay: rotation[rotation.findIndex((li) => li.id == known.id)].id === 193234 ? 60 : 30,
    };

    while (cur.date < now3) {
      cur = next(cur);
    }

    const out = document.getElementById("test");

    while (cur.date < end && cur.date < now2 && cur.date > now3) {
      const li = document.createElement("li");
      li.className = "test";

      var link = document.createElement("a");
      link.textContent = `${rotation[cur.index].name}`;
      link.href = "dadada";
      link.class = "dadada";

      li.innerText = `${rotation[cur.index].name} (${cur.date.toLocaleString()})`;

      var id = `${rotation[cur.index].id}`;
      var title2 = `${rotation[cur.index].name}`;
      var link = "https://de.wowhead.com/npc=";
      var ClassName = "weeklytext";
      var ClassName2 = "weeklytext2";
      var ClassName3 = "peinigertime";

      var options = { hour: "numeric", minute: "numeric" };

      var date33 = `${cur.date.toLocaleString("en-GB", options)}`;

      $(
        '<li class="' +
          ClassName2 +
          '"><div class="' +
          ClassName3 +
          '"> ' +
          date33 +
          '</div> <a class="' +
          ClassName +
          '" href="' +
          link +
          id +
          '">' +
          title2 +
          "</a></li>"
      ).appendTo(out);

      cur = next(cur);
    }
  }
});

相关问题