1

我想先显示一条消息,然后重定向我的页面:

toastr.success('Your report has been submitted successfully.');
window.location = '/Report';

但是上面的代码允许两个语句同时运行。结果,重定向发生在显示 toastr 消息之前,这需要时间。

$.when(...).done(...)对于这种情况,我们有类似的东西吗?

- - 更新 - -

如果有人不知道toastr,它是一个在网页上显示消息的库。

当然设置一个计时器会起作用。但是有没有什么优雅的方法可以在第一个语句完成后运行第二个语句?

4

3 回答 3

4

您可以使用 setTimeout :

toastr.success('Your report has been submitted successfully.');
setTimeout(function(){window.location = '/Report';}, 2000);
于 2012-09-12T15:06:14.410 回答
0

您可以将函数作为回调传递,以便在函数完成后执行。请参阅有关此类似问题的所选答案:我应该如何调用 3 个函数以便一个接一个地执行它们?

于 2013-10-01T13:56:48.637 回答
0

onHidden使用 toastr选项我取得了很大的成功。它基本上是一个事件回调,允许您设置重定向 url,在 toast 关闭或隐藏时触发。

// Notify the user of success and redirect when the toast closes
toastr.options.onHidden = function () {
     window.location.href = <YOUR REDIRECT URL>;
};

// Fire the success toast which is bound to the onHidden function                    
toastr.success('Success!<br/><br/>You\'ll now be automatically redirected to your current items.', 'Download Success', { timeOut: 3000, closeButton: true, positionClass: 'toast-bottom-right', progressBar: true });
于 2016-12-03T00:32:19.807 回答