gpt4 book ai didi

javascript - 当 settimeout == 0 时改变页面

转载 作者:行者123 更新时间:2023-12-04 03:44:22 25 4
gpt4 key购买 nike

我有这个函数,我想要在时间等于 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) {//我用了很多东西,但我没有管理好

我在 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 - 当 settimeout == 0 时改变页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65417567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com