javascript - 当 setimeout == 0 时更改页面
问题描述
我有这个功能,我想要时间等于 0。我想让我的页面从 index.php 更改为 gameover.html。
function progress(timeleft, timetotal, $element) {
var progressBarWidth = timeleft * $element.width() / timetotal;
$element
.find('div')
.animate({ width: progressBarWidth }, 500)
.html(timeleft + " seconds to go");
if(timeleft > 0) {
setTimeout(function() {
progress(timeleft - 1, timetotal, $element);
}, 1000);
}
};
progress(180, 180, $('#progressBar'));
我想,到那个 if(timeleft==0) {//i use much things but i didn't managed }
。
我在 index.php 上的 php 代码,所以我将能够连接到我的数据库
session_start();
include("s/connect.php");
if($_SERVER['REQUEST_METHOD'] == "POST")
{
//something was posted
$user_name = $_POST['user_name'];
$password = $_POST['password'];
if(!empty($user_name) && !empty($password) && !is_numeric($user_name))
{
$user_id = random_num(20);
$query = "insert into users ( user_id,user_name,password) values ('$user_id','$user_name','$password')";
mysqli_query($mysqli, $query);
header("Location: gameover.html");
die;
}
else{
echo "Please enter some valid information!";
}
}
解决方案
试试这个(如果 timeleft 不 >0,做一个重定向)
<script>
function progress(timeleft, timetotal, $element) {
var progressBarWidth = timeleft * $element.width() / timetotal;
$element
.find('div')
.animate({ width: progressBarWidth }, 500)
.html(timeleft + " seconds to go");
if(timeleft > 0) {
setTimeout(function() {
progress(timeleft - 1, timetotal, $element);
}, 1000);
}else{
window.location.href='gameover.html';
}
};
progress(180, 180, $('#progressBar'));
</script>
推荐阅读
- javascript - Uncaught (in promise) TypeError: this.getChildrem is not a function
- arduino - 最大化 Arduino Nano 33 BLE Sense 的 Serial.write() 数据传输速度
- python - 加载 tensorflow 模型时出错 - TypeError: __init__() got an unexpected keyword argument 'axis'
- javascript - 将颜色应用于单击的表格行
- html - 如何创建适合所有屏幕尺寸的 Web 应用程序?
- html - 内容安全策略元标记“不安全内联”不起作用
- sql-server - SQL 触发器的想法
- c# - HttpContext SignInAsync 因 Cookie 和声明而失败
- flutter - DropDownFormField 无法从 api 初始化对象列表,请任何人帮助无法解决
- pandas - 根据熊猫中的数值范围添加累积值