首页 > 解决方案 > 执行完最后一个函数后如何刷新页面?

问题描述

在我按下回车键后,我正在尝试创建一个 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>

标签: javascripthtmlcss

解决方案


只需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);
}

推荐阅读