我会尽量简短。
我对编程几乎一无所知,我正在尝试自己学习一个个人项目。我正在尝试做一个类似计时器的事情,当它达到0时,将重定向到另一个页面。到目前为止,我设法构建的是:
<!DOCTYPE html>
<html>
<body>
<input type="text" id="txt">
<body onload="startCount()">
<script>
var c = 10;
var t ;
var timer_is_on = 0;
function timedCount() {
document.getElementById("txt").value = c;
c = c - 1;
t = setTimeout(timedCount, 1000);
}
function startCount() {
if (!timer_is_on) {
timer_is_on = 0;
timedCount();
}
}
</script>
</body>
</html>
当我在chrome上尝试时,它无法运行,或者会立即跳至另一页。如果您能告诉我什么地方出了问题或如何解决,那就太好了。
现在,我只是发现(?)我丢失了重定向的另一部分。我用这个
<script>
function myFunction() {
location.replace("https://www.w3schools.com")
}
</script>
我不知道我是否正确解释了这一点,但我感谢您的帮助!
谢谢!
在timedCount中检查C的值是否大于0,否则运行setTimeout否则(小于或等于0)重定向
此外,我认为您打算在此处将timer_is_on更新为1:
function startCount() {
if (!timer_is_on) {
timer_is_on = 0; //Shouldn't this be set to 1?
timedCount();
}
}