javascript 404页面自定义重定向失败

javascript 404页面自定义重定向失败,javascript,html,Javascript,Html,代码: 或者等待五秒钟。。。。。。 setTimeout(function(){document.getElementById('time').innerHTML=“Four”},1000); setTimeout(function(){document.getElementById('time').innerHTML=“Three”},1000); setTimeout(function(){document.getElementById('time').innerHTML=“Two”},1

代码:


或者等待五秒钟。。。。。。
setTimeout(function(){document.getElementById('time').innerHTML=“Four”},1000);
setTimeout(function(){document.getElementById('time').innerHTML=“Three”},1000);
setTimeout(function(){document.getElementById('time').innerHTML=“Two”},1000);
setTimeout(function(){document.getElementById('time').innerHTML=“One”},1000);
setTimeout(函数(){window.location.href=”http://tool-box.weebly.com"}",1000);
不成功。我该怎么办??
请帮助。Thx。

设置超时时间都是同时排队的,因此您需要指定时间,并将其考虑在内:

<html>
<body>

<a href="index.html">Click Here to Back to Home Page</a> Or Wait <p id="time" style="display:inline">Five</p> Seconds......
<script>
setTimeout(function(){document.getElementById('time').innerHTML="Four"},1000);
setTimeout(function(){document.getElementById('time').innerHTML="Three"},1000);
setTimeout(function(){document.getElementById('time').innerHTML="Two"},1000);
setTimeout(function(){document.getElementById('time').innerHTML="One"},1000);
setTimeout(function(){window.location.href="http://tool-box.weebly.com"}",1000);
</script>

</body>
</html>

或者等待五秒钟。。。。。。
setTimeout(function(){document.getElementById('time').innerHTML=“四秒”},1000);
setTimeout(function(){document.getElementById('time').innerHTML=“3秒”},2000);
setTimeout(function(){document.getElementById('time').innerHTML=“两秒”},3000);
setTimeout(function(){document.getElementById('time').innerHTML=“1秒”},4000);
setTimeout(函数(){window.location.href=”http://tool-box.weebly.com"},5000);

所有的
setTimeout
s同时排队,因此您需要指定时间,并将其考虑在内:

<html>
<body>

<a href="index.html">Click Here to Back to Home Page</a> Or Wait <p id="time" style="display:inline">Five</p> Seconds......
<script>
setTimeout(function(){document.getElementById('time').innerHTML="Four"},1000);
setTimeout(function(){document.getElementById('time').innerHTML="Three"},1000);
setTimeout(function(){document.getElementById('time').innerHTML="Two"},1000);
setTimeout(function(){document.getElementById('time').innerHTML="One"},1000);
setTimeout(function(){window.location.href="http://tool-box.weebly.com"}",1000);
</script>

</body>
</html>

或者等待五秒钟。。。。。。
setTimeout(function(){document.getElementById('time').innerHTML=“四秒”},1000);
setTimeout(function(){document.getElementById('time').innerHTML=“3秒”},2000);
setTimeout(function(){document.getElementById('time').innerHTML=“两秒”},3000);
setTimeout(function(){document.getElementById('time').innerHTML=“1秒”},4000);
setTimeout(函数(){window.location.href=”http://tool-box.weebly.com"},5000);

您遇到的问题是
setTimeout()
是异步的。它安排一些代码在将来某个时间运行。您编写代码的方式看起来像是期望
setTimeout()
暂停JavaScript的执行直到超时发生,然后继续执行下一条语句。但是,这样做不起作用。您已计划将来执行5行代码,未来每1000毫秒执行一行

要解决这个问题,您可以将每条语句的超时时间增加到比前一条语句长1000毫秒。但是,所有这些冗余代码都不是很好。相反,我会使用递归函数每秒写入一次数组元素,直到数组为空,然后重定向:

<html>
<body>

<a href="index.html">Click Here to Back to Home Page</a> Or Wait <p id="time" style="display:inline">Five Seconds</p>......
<script>
setTimeout(function(){document.getElementById('time').innerHTML="Four Seconds"},1000);
setTimeout(function(){document.getElementById('time').innerHTML="Three Seconds"},2000);
setTimeout(function(){document.getElementById('time').innerHTML="Two Seconds"},3000);
setTimeout(function(){document.getElementById('time').innerHTML="One Second"},4000);
setTimeout(function(){window.location.href="http://tool-box.weebly.com"},5000);
</script>

</body>
</html>

您遇到的问题是
setTimeout()
是异步的。它会安排一些代码在将来的某个时间运行。您编写代码的方式看起来就像您期望的
setTimeout()
暂停JavaScript的执行直到超时发生,然后继续执行下一条语句。但是,这样做不起作用。您已计划将来执行5行代码,未来每1000毫秒执行一行

要解决这个问题,您可以将每条语句的超时时间增加到比前一条语句长1000毫秒。但是,所有这些冗余代码都不是很好。相反,我会使用递归函数每秒写入一次数组元素,直到数组为空,然后重定向:

<html>
<body>

<a href="index.html">Click Here to Back to Home Page</a> Or Wait <p id="time" style="display:inline">Five Seconds</p>......
<script>
setTimeout(function(){document.getElementById('time').innerHTML="Four Seconds"},1000);
setTimeout(function(){document.getElementById('time').innerHTML="Three Seconds"},2000);
setTimeout(function(){document.getElementById('time').innerHTML="Two Seconds"},3000);
setTimeout(function(){document.getElementById('time').innerHTML="One Second"},4000);
setTimeout(function(){window.location.href="http://tool-box.weebly.com"},5000);
</script>

</body>
</html>

最后一行有一个语法错误,一个额外的引号字符:

var countEl = document.getElementById("time");
var count = ["Five", "Four", "Three", "Two", "One"];
(function countDown() {
    if (count.length) {
        countEl.innerHTML = count.shift();
        setTimeout(countDown, 1000);
    }
    else {
        location.href = "http://tool-box.weebly.com";
    }
})();

修复此问题将允许脚本“工作”,但新页面仅在1秒后加载:对于倒计时,您需要为每个后续超时指定更长的时间间隔,否则它们都将在代码运行1000毫秒后排队执行。
setTimeout()
不会暂停当前函数的执行或休眠,它只是为以后的操作排队。因此,请指定1000、2000、3000等,与其他答案相同。

最后一行出现语法错误,一个额外的引号字符:

var countEl = document.getElementById("time");
var count = ["Five", "Four", "Three", "Two", "One"];
(function countDown() {
    if (count.length) {
        countEl.innerHTML = count.shift();
        setTimeout(countDown, 1000);
    }
    else {
        location.href = "http://tool-box.weebly.com";
    }
})();

修复此问题将允许脚本“工作”,但新页面仅在1秒后加载:对于倒计时,您需要为每个后续超时指定更长的时间间隔,否则它们都将在代码运行1000毫秒后排队执行。
setTimeout()
不会暂停当前函数的执行或休眠,它只是为以后的操作排队。因此,在其他答案中指定1000、2000、3000等。

发生了什么?您期望发生什么?发生了什么?您期望发生什么?