Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

jquery - Page Redirect after X seconds wait using 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));

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.

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...