我有一个脚本,它应该在常量(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);
}
}
});