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

Page Redirect after X seconds wait using JavaScript

本文关键字:重定向 JavaScript 等待 使用      更新时间:2023-09-26

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

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

但它不会等待 5 秒。比我在谷歌中搜索问题,而不是知道"document.ready(("是在DOM加载文档时调用的,而不是在Web浏览器上。

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

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

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

示例脚本和现场演示可以从这里找到 - 使用 JavaScript 延迟后重定向页面

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

/document.write("您将在 1000 -> 1 秒、2000 -> 2 秒内重定向到新页面"(;

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