Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I redirect after delay() with jQuery?

I'm using jQuery delay() function to delay a show() event, but after that I want to change the location.href of the page. How can I do that?

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

Sorry, I forgot to mention I want to delay the redirect as well.

like image 412
Guilherme David da Costa Avatar asked Sep 25 '12 05:09

Guilherme David da Costa


1 Answers

Provide a callback for show()

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

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

The 'show' will happen after 800 milliseconds, then after the element is shown, the redirect will occur 8 seconds later. With this code you will have 2 'delays' so to speak.

like image 63
Mark Avatar answered Oct 04 '22 00:10

Mark