How can I redirect after delay () using jQuery?

I use the jQuery function delay()to delay the event show(), but after that I want to change the location.href on the page. How can i do this?

$('#error').delay(800).show();
location.href = '/profile'

Sorry, I forgot to mention that I also want to postpone the redirect.

+5
source share
2 answers

Provide callback for show ()

$('#error').delay(800).show(0, function () {
    setTimeout(function () {
        location.href = '/profile'
    }, 8000);
});

.show () documentation http://api.jquery.com/show/

The "show" will occur in 800 milliseconds, after the element is shown, the redirection will occur in 8 seconds. With this code you will have 2 "delays", so to speak.

+8
source

window.setTimeout() , .delay jQuery.

$('#error').show();
setTimeout(function() {
  location.href = '/profile';
}, 800);
+1

All Articles