简体   繁体   English

如何使用jQuery在delay()之后重定向?

[英]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. 我正在使用jQuery delay()函数来延迟show()事件,但之后我想更改页面的location.href。 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. 对不起,我忘了提及我也想延迟重定向。

Provide a callback for show() 为show()提供回调

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

documentation on .show() http://api.jquery.com/show/ 关于.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. 'show'将在800毫秒后发生,然后在显示元素后,重定向将在8秒后发生。 With this code you will have 2 'delays' so to speak. 使用此代码,您可以说有2个“延迟”。

Use window.setTimeout() instead, .delay could only be used to jQuery animation. 使用window.setTimeout()代替, .delay只能用于jQuery动画。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM