我有一个脚本,它应该在常量(const rotation)的值之间旋转.它应该从某个值和开始日期(const known)开始连续运行和启动

列表中的下一个值应该总是在30分钟之后出现,但有一个例外:如果值的"id"是"193234"(Eldoren),则下一个值应该总是在60分钟(而不是30分钟)之后出现.

问题是,这个例外只适用于id"193234"的第一次出现,而不适用于后面的出现:

Output:

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

这是我的 playbook 和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);
            }
          }
        });

推荐答案

您的脚本中"Eldoren"ID的异常处理似乎有问题.问题存在于更新incresteMinents变量的逻辑中.

  1. 该脚本将increeMinmins初始化为0.
  2. 第一次遇到"Eldoren"时,条件id== 193234为真,并且increeneMinmins设置为30.
  3. 对于后续值,条件增加分钟>;0始终为 FALSE,因为increeMinents保持为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);
    }
  }
});

Javascript相关问答推荐

我的YouTube视频没有以html形式显示,以获取免费加密信号

使用CDO时如何将Vue组件存储在html之外?

从PWA中的内部存储读取文件

jQuery提交按钮重新加载页面,即使在WordPress中使用preventDefault()

按下同意按钮与 puppeteer 师

在nextjs服务器端api调用中传递认证凭证

你怎么看啦啦队的回应?

使用POST请求时,Req.Body为空

如何发送从REST Api收到的PNG数据响应

Web Crypto API解密失败,RSA-OAEP

在使用位板时,如何在Java脚本中判断Connect 4板中中柱的对称性?

使用Nuxt Apollo在Piniastore 中获取产品细节

在JS中动态创建对象,并将其追加到HTML表中

如何在Java脚本中对列表中的特定元素进行排序?

删除元素属性或样式属性的首选方法

如何在尚未创建的鼠标悬停事件上访问和着色div?

如何在下一个js中更改每个标记APEXCHARTS图表的 colored颜色

在JavaScript中将Base64转换为JSON

有角粘桌盒阴影

在传单的图像覆盖中重新着色特定 colored颜色 的所有像素