页面redirectX秒后等待使用JavaScript

input错误信息5秒后,我需要redirect到特定的URL。 首先我使用Javascript如下。

document.ready(window.setTimeout(location.href = "https://www.google.co.in",5000)); 

但它并不等待5秒钟。 比我在谷歌search问题比来知道,“document.ready()”时调用文档加载在DOM,而不是在网页浏览器。

比我使用jQuery的window.load()函数,但仍然没有得到我想要的。

 $(window).load(function() { window.setTimeout(window.location.href = "https://www.google.co.in",5000); }); 

任何人都可以请让我知道我到底需要等5秒钟。

看起来你几乎在那里。 尝试:

 if(error == true){ // Your application has indicated there's an error window.setTimeout(function(){ // Move to a new location or you can do something else window.location.href = "https://www.google.co.in"; }, 5000); } 

你实际上需要在你想在5000毫秒后执行的window.setTimeout()传递一个函数,如下所示:

 $(document).ready(function () { // Handler for .ready() called. window.setTimeout(function () { location.href = "https://www.google.co.in"; }, 5000); }); 

更多信息: .setTimeout()

你可以使用这个javascript函数。 在这里,您可以向用户显示redirect消息,并redirect到给定的URL。

 <script type="text/javascript"> function Redirect() { window.location="http://www.newpage.com"; } document.write("You will be redirected to a new page in 5 seconds"); setTimeout('Redirect()', 5000); </script> 

你需要传递一个函数setTimeout

 $(window).load(function () { window.setTimeout(function () { window.location.href = "https://www.google.co.in"; }, 5000) }); 
 $(document).ready(function() { window.setTimeout(function(){window.location.href = "https://www.google.co.in"},5000); }); 

使用JavaScript setInterval()方法在指定的时间之后redirect页面。 以下脚本将在5秒后redirect页面。

 var count = 5; setInterval(function(){ count--; document.getElementById('countDown').innerHTML = count; if (count == 0) { window.location = 'https://www.google.com'; } },1000); 

示例脚本和现场演示可以从这里find – 使用JavaScript延迟后redirect页面