【问题标题】:How do I automatically Start the timer when I reload this Page?重新加载此页面时如何自动启动计时器?
【发布时间】:2020-07-14 02:35:32
【问题描述】:
<p id="countdown"></p>
  <div id="startButtons">
    <button onclick="sample()" id="gameStart" class="gameStart">LETS GO!</button>

    <script>
      var timeleft = 90;
      window.onload = timedText;
      function sample() {
        document.getElementById('paragraph').innerHTML = "<style>.paragraph(visibility:collapse);";
        document.getElementById('gameStart').innerHTML = "<style>.gameStart(visibility:collapse);";
        var downloadTimer = setInterval(function
          function1() {
          document.getElementById("countdown").innerHTML = timeleft + "&nbsp" + "seconds left";

          timeleft -= 1;
          if (timeleft <= 0) {
            clearInterval(downloadTimer);

            document.getElementById("countdown").innerHTML = "The time has ended!";

            window.open("index2.1.html");
          }
        }, 1000);

        console.log(countdown)

      }

    </script>

我在做这个文字游戏,当我重新加载页面时,我真的不知道如何启动计时器,我希望计时器自己启动,所以如果我在主页上单击 Enter 并到达此页面计时器应该自行启动......,当计时器完成时,另一个窗口应该打开,即 2.1 index.html 谢谢

【问题讨论】:

    标签: javascript java html css timer


    【解决方案1】:

    与其将 JavaScript 循环包装在一个需要在页面加载时调用的函数中,不如直接运行 JavaScript(无需放入函数中),页面将在读取/加载它时立即运行它。

    <p id="countdown">Countdown Beginning</p>
    
    <script>
      var timeleft = 90;
      var downloadTimer = setInterval(function () {
        document.getElementById("countdown").innerHTML = `${timeleft} seconds left`;
        timeleft -= 1;
        if (timeleft <= 0) {
          clearInterval(downloadTimer);
          document.getElementById("countdown").innerHTML = "The time has ended!";
        }
      }, 1000);
    </script>

    【讨论】:

    • 代码可以工作,但是当时间结束时页面没有改变,你能帮我吗
    • 添加您的window.open("index2.1.html") 应该可以工作??‍♂️
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 2016-12-08
    • 2018-04-06
    • 2021-03-28
    相关资源
    最近更新 更多