javascript - 如何在执行完最后一个功能后刷新我的页面?

标签 javascript html css

我试图在按下回车键后创建一个 javascript 程序,它将完成程序的执行,然后我的页面将刷新。我尝试在我的最后一个函数中添加一个 if 循环,但它不起作用。

希望有人能帮助我完成编码。因为我真的是 HTML CSS 和 javascript 的新手。

最下面是我的程序:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <!-- <meta http-equiv="refresh" content="30"> -->
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>Document</title>
<style>
  body {
    height: 100%;
    width: 100%;
    background-image: url("TPHRG floorplan1.png");
    background-repeat: no-repeat;
    background-attachment: fixed;
    /* background-position: center; */
    background-size: 980px 400px, cover;
  }

  .robot_start_top {
    top: 280px;
    transition: top 2s;
  }

  .robot_start_left {
    position: fixed;
    left: 600px;
    transition: all 2s;
  }

  .robot_end_left {
    left: 570px;
  }

  .robot_end_top {
    top: 180px;
  }

  .robot1_start_left {
    position: fixed;
    left: 570px;
    transition: left 4s;
  }

  .robot1_end_left {
    left: 520px;
  }

  .robot2_start_left {
    position: fixed;
    left: 520px;
    transition: left 4s;
  }

  .robot2_end_left {
    left: 470px;
  }
  .robot3_start_left {
    position: fixed;
    left: 470px;
    transition: left 4s;
  }

  .robot3_end_left {
    left: 420px;
  }

  .robot3_start_right {
    position: fixed;
    left: 470px;
    transition: left 4s;
  }
  .robot3_start_down {
    position: fixed;
    left: 180px;
    transition: left 4s;
  }

  .robot3_end_down {
    top: 280px;
  }

  .robot3_end_right {
    left: 570px;
  }
</style>
</head>
<body onkeydown="move(event)">
<div class="robot_start_left robot_start_top" id="app">
  <img id="robot" style= width:30px; height:40px" src="pic_8.PNG">
</div>

<script>
  var move = function(event) {
    if (event.keyCode === 97) {
      const appDiv = document.getElementById("app");
      setTimeout(function() {
        appDiv.classList.add("robot_end_top");
      }, 0);
      setTimeout(function() {
        appDiv.classList.add("robot_end_left");
      }, 2000);
    }

    if (event.keyCode === 98) {
      const appDiv = document.getElementById("app");
      setTimeout(function() {
        appDiv.classList.add("robot_end_top");
      }, 0);
      setTimeout(function() {
        appDiv.classList.add("robot1_end_left");
      }, 2000);
    }

    if (event.keyCode === 99) {
      const appDiv = document.getElementById("app");
      appDiv.classList.add("robot2_end_left");
    }

    if (event.keyCode === 100) {
      const appDiv = document.getElementById("app");
      appDiv.classList.add("robot3_end_left");
    }

      if (event.keyCode === 13) {
        const appDiv = document.getElementById("app");
        setTimeout(function() {
          appDiv.classList.add("robot3_end_down");
        }, 2000);
        setTimeout(function() {
          appDiv.classList.add("robot3_end_right");
        }, 0);

        window.location = '';
      }

  }
</script>
</body>
</html>

最佳答案

只需使用 window.location.reload(true)在你的函数结束时

您可能必须使用 setTimeout(window.location.reload(true), 0) 将重新加载设置到执行堆栈的末尾,但是

var move = function (event) {
    if (event.keyCode === 97) {
        // Code
    }

    if (event.keyCode === 98) {
        // Code
    }

    if (event.keyCode === 99) {
        // Code
    }

    if (event.keyCode === 100) {
        // Code
    }

    if (event.keyCode === 13) {
        // Code
    }

    setTimeout(window.location.reload(true), 0);
}

关于javascript - 如何在执行完最后一个功能后刷新我的页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59491117/

相关文章:

javascript - 如何从 chrome 扩展 popup.html 中重新加载当前选项卡?

html - 每个 CSS3 框架重载比较?

javascript - 访问另一个 JS 文件中的数组 - Chrome 扩展

javascript - 使用google脚本操作数据条件格式和数据验证

javascript - onsubmit 后如何清除纯 HTML 表单?

php - 如何将样式应用于 ISSET 为真时我将回显的 PHP session 变量

javascript - 我如何控制来自另一个 html 的元素?

css - 内联 CSS 来制作列

javascript - 尝试使用 id==valores0 注册小部件,但该 id 已注册

javascript - 从本地数据库加载所有数据