使用JavaScript等待X秒后的页面重定向 [英] Page Redirect after X seconds wait using JavaScript

查看:100
本文介绍了使用JavaScript等待X秒后的页面重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在输入错误消息后5秒后重定向到特定网址。首先,我使用了以下Javascript。

I need to redirect to specific url after 5 seconds after putting an error message. First i have used Javascript as below.

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

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

But it is not waiting for 5 seconds. Than I searched the issue in google than come to know that "document.ready()" is invoked when document is loaded at DOM, not at web browser.

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

Than i have used window.load() function of jQuery but still i am not getting what i want.

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

任何人都可以让我知道我需要做什么才能等待5秒钟。

Can anyone please let me know how exactly i need to do to wait for 5 seconds.

推荐答案

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

It looks you are almost there. Try:

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等待X秒后的页面重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆