Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaScript在X秒等待后进行页面重定向_Javascript_Jquery - Fatal编程技术网

使用JavaScript在X秒等待后进行页面重定向

使用JavaScript在X秒等待后进行页面重定向,javascript,jquery,Javascript,Jquery,我需要重定向到特定的url后5秒后,把一个错误消息。首先,我使用了Javascript,如下所示 document.ready(window.setTimeout(location.href = "https://www.google.co.in",5000)); 但它不会等待5秒。然后我在google中搜索了这个问题,然后才知道“document.ready()”是在DOM中加载文档时调用的,而不是在web浏览器中 我使用了jQuery的window.load()函数,但仍然没有得到我想要的

我需要重定向到特定的url后5秒后,把一个错误消息。首先,我使用了Javascript,如下所示

document.ready(window.setTimeout(location.href = "https://www.google.co.in",5000));
但它不会等待5秒。然后我在google中搜索了这个问题,然后才知道“document.ready()”是在DOM中加载文档时调用的,而不是在web浏览器中

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

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

任何人都可以告诉我等待5秒钟需要做什么。

您实际上需要在
窗口内传递一个函数。setTimeout()
在5000毫秒后要执行该函数,如下所示:

$(document).ready(function () {
    // Handler for .ready() called.
    window.setTimeout(function () {
        location.href = "https://www.google.co.in";
    }, 5000);
});
var count = 5;
setInterval(function(){
    count--;
    document.getElementById('countDown').innerHTML = count;
    if (count == 0) {
        window.location = 'https://www.google.com'; 
    }
},1000);

有关更多信息:

您需要将函数传递给
setTimeout

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

看来你快到了。尝试:

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);

}

您可以使用这个JavaScript函数。在这里,您可以向用户显示重定向消息,并重定向到给定的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>

函数重定向()
{  
window.location=”http://www.newpage.com"; 
} 
编写(“您将在5秒钟内重定向到新页面”);
setTimeout('Redirect()',5000);

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

var count = 5;
setInterval(function(){
    count--;
    document.getElementById('countDown').innerHTML = count;
    if (count == 0) {
        window.location = 'https://www.google.com'; 
    }
},1000);
示例脚本和实时演示可以在这里找到-


函数idleTimer(){
变量t;
//window.onload=重置计时器;
window.onmousemove=resetTimer;//捕捉鼠标移动
window.onmousedown=resetTimer;//捕捉鼠标移动
window.onclick=resetTimer;//捕捉鼠标单击
window.onscroll=resetTimer;//捕获滚动
window.onkeypress=resetTimer;//捕捉键盘操作
函数注销(){
window.location.href='logout.php';//适应实际的注销脚本
}
函数重载(){
window.location=self.location.href;//重新加载当前页面
}
函数resetTimer(){
清除超时(t);
t=setTimeout(注销,1800000);//时间单位为毫秒(1000为1秒)
t=setTimeout(重载,300000);//时间单位为毫秒(1000为1秒)
}
}
idleTimer();

//编写(“您将在1000->1秒、2000->2秒后重定向到新页面”)

比我快。重定向不会等待,因为正在立即分配位置。对于一个完整的解决方案,它必须是一个函数调用:虽然这是一个javascript问题,但似乎应该提到HTML替代方案<代码>一个html替代方案谢谢,我将它与PHP结合使用:if($row_cnt>0){echo“已经发布!”;echo“;exit();}
<script type="text/javascript">
      function idleTimer() {
    var t;
    //window.onload = resetTimer;
    window.onmousemove = resetTimer; // catches mouse movements
    window.onmousedown = resetTimer; // catches mouse movements
    window.onclick = resetTimer;     // catches mouse clicks
    window.onscroll = resetTimer;    // catches scrolling
    window.onkeypress = resetTimer;  //catches keyboard actions

    function logout() {
        window.location.href = 'logout.php';  //Adapt to actual logout script
    }

   function reload() {
          window.location = self.location.href;  //Reloads the current page
   }

   function resetTimer() {
        clearTimeout(t);
        t = setTimeout(logout, 1800000);  // time is in milliseconds (1000 is 1 second)
        t= setTimeout(reload, 300000);  // time is in milliseconds (1000 is 1 second)
    }
}
idleTimer();
        </script>
setTimeout('Redirect()', 1000);
function Redirect() 
{  
    window.location="https://stackoverflow.com"; 
}