javascript - 执行完最后一个函数后如何刷新页面?
问题描述
在我按下回车键后,我正在尝试创建一个 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);
}
推荐阅读
- image - 在TImageViewer中,如何获取用户点击图片的位置?
- maven - 从原型创建 Maven 项目时出错:需要 501 HTTPS
- python - Python请求查找字符串
- reactjs - 使用 require 后删除 scss 文件
- c# - Redis缓存的序列化列表和大对象
- html - 背景图像太放大
- php - 如何在 yii2 中设置 elfinder 的 URL?
- flutter - tabbarview 中的小部件需要额外的底部间距。如何根据内容证明它们的高度?
- batch-file - 如何为我的 .wem 转换器设置输出文件夹?
- php - 如何从邮递员那里获取参数以将 Laravel 中的值保存到数据库中