执行上一个功能后如何刷新页面?

时间:2019-12-26 16:43:42

标签: javascript html css

按下Enter键后,我试图创建一个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>

1 个答案:

答案 0 :(得分:1)

只需在函数末尾使用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);
}