【问题标题】:Sleep , freeze, end?睡眠,冻结,结束?
【发布时间】:2022-01-05 07:45:18
【问题描述】:

你能帮我解决这个问题吗?我只想发送一个命令,但要尽可能准确(以毫秒为单位)。 这是我需要修复的部分:

document.getElementById("arrTime").onclick = function () {
    clearInterval(attInterval);
    let time = document.getElementsByClassName("relative_time")[0].textContent.slice(-8);
    input = prompt("Set Arrival Time", time);
    inputMs = parseInt(prompt("Set the milliseconds", "500"));
    delay = parseInt(delayTime) + parseInt(inputMs);
    let arrivalTime;
    arrInterval = setInterval(function () {
        arrivalTime = document.getElementsByClassName("relative_time")[0].textContent;
        if (arrivalTime.slice(-8) >= input) {
            setTimeout(function () { document.getElementById("troop_confirm_submit").click(); }, delay);
        }
    }, 5);
    document.getElementById("showArrTime").innerHTML = input + ":" + inputMs.toString().padStart(3, "0");
    document.getElementById("showSendTime").innerHTML = "";
};

现在,有一个“if 语句”可以在 arrivalTime.slice(-8) >= input 执行操作(例如 19:24:30),但它每隔5毫秒。因此,在一秒钟内,它向服务器发送了 200 个请求。 我不想更改这 5 毫秒,因为我需要让它尽可能准确,但我想在执行命令后中断脚本、冻结或休眠 1 秒。所以类似: setTimeout(function () { document.getElementById("troop_confirm_submit").click(); Sleep 1 seconds }, delay);

谁能帮忙?

【问题讨论】:

  • 注意if (arrivalTime.slice(-8) >= input) { 比较的是两个字符串而不是数字
  • 也是clearInterval(attInterval)应该是clearInterval(arrInterval)
  • 还请注意,如果您已经在自己的文件中编写 JS,则没有理由使用旧的 onclick 属性。作为 HTML 元素属性的旧“JS 镜像”,它在现代 JS 中没有立足之地。请改用.addEventListener("click", ...)
  • 嗨,我知道。它应该在特定时间执行命令,所以一旦到达时间.slice(-8) 是 f.e. 19:30:00。但问题是,19:30:00 持续一秒,所以字符串持续 1 秒,每 5 毫秒刷新一次。因此,在那一秒钟内,它发送了 200 个请求。是的,应该是 clearInterval(attInterval)。这是另一个功能,在这种情况下可以忽略
  • 如果你依赖时钟时间:不要检查点击时间,检查“我上次检查是什么时候,现在是什么时候,以及距离我实际时间有多少秒关心它已经“。当您的 JS 没有在焦点上运行时,它的计时器在现代浏览器中的优先级会大大降低,并且间隔之间可能长达 30 秒,无论您将其设置为什么。超时保证至少只要您请求,但不能保证即使在接近该时间的任何地方也会触发。

标签: javascript sleep break


【解决方案1】:

我建议分解函数以便更轻松地管理间隔

document.getElementById("arrTime").onclick = function() {
  clearInterval(attInterval);
  let time = document.getElementsByClassName("relative_time")[0].textContent.slice(-8);
  input = prompt("Set Arrival Time", time);
  inputMs = parseInt(prompt("Set the milliseconds", "500"));
  delay = parseInt(delayTime) + parseInt(inputMs);
  startInterval(input, delay)
  document.getElementById("showArrTime").innerHTML = input + ":" + inputMs.toString().padStart(3, "0");
  document.getElementById("showSendTime").innerHTML = "";
};

function startInterval(input, delay) {
  arrInterval = setInterval(function() {
    let arrivalTime = document.querySelector(".relative_time").innerText;
    if (+arrivalTime.slice(-8) >= +input) {
      clearInterval(arrInterval); // pause the interval
      setTimeout(() => {
        startInterval(input, delay)
      }, 1000 * 60); // restart the interval in 1 minute       
      setTimeout(() => {
        document.querySelector("#troop_confirm_submit").click();
      }, delay);
    }
  }, 5);
}

【讨论】:

  • 这不是我需要的。当我使用它时,输入一些时间,f.e. 21:05:00 和 ms 500,它不执行 document.querySelector("#troop_confirm_submit").click(),即到达时间.slice(-8) >= input跨度>
  • ``` document.getElementById("arrTime").onclick = function () { clearInterval(attInterval);让时间 = document.getElementsByClassName("relative_time")[0].textContent.slice(-8); input = prompt("请输入您希望到达的时间", time); inputMs = parseInt(prompt("请输入大约毫秒", "500"));延迟 = parseInt(delayTime) + parseInt(inputMs); document.getElementById("showArrTime").innerHTML = input + ":" + inputMs.toString().padStart(3, "0"); document.getElementById("showSendTime").innerHTML = "";集合到达时间(); }; ```
  • 函数 setArrivalTime() { 让到达时间; arrInterval = setInterval(function () {arrivalTime = document.getElementsByClassName("relative_time")[0].textContent; if (arrivalTime.slice(-8) >= input) { setTimeout(function () { document.getElementById("troop_confirm_submit ").click(); }, 延迟); } }, 5); }
  • 我将它拆分为 2 个功能,一切正常,只是不只执行 1 个命令
  • 但是 200 因为它每 5 毫秒执行一次命令,持续 1 秒
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-04
  • 1970-01-01
  • 2011-08-06
  • 1970-01-01
  • 2016-09-22
  • 2019-01-28
  • 1970-01-01
相关资源
最近更新 更多