Settimeout() 不同步工作

Settimeout() not working synchronusly

我有一个页面,点击了 submitbutton.When 提交按钮,我需要显示一个带有一些通知消息的对话框 2 seconds.After 2 秒,我需要将页面重定向到另一个位置。

setTimeout(function(){
       $('#wrapper_dialog').dialog('close');                
                                }, 2000);
//after this popup closes,I need to redirect to another page
 window.location = "?load=parents/communication";

当我这样做时,出现对​​话框,页面立即异步引导到下一页。

setTimeout(function(){
$('#wrapper_dialog').dialog('close');}, 2000).then(function(){
    window.location = "?load=parents/communication";
    });

这不起作用。

setTimeout 不是 return 承诺,它 return 是一个数字;所以上面没有 then 函数。

只需将 location 赋值放在关闭对话框的调用之后, setTimeout 回调中:

setTimeout(function(){
    $('#wrapper_dialog').dialog('close');                
    window.location = "?load=parents/communication";
}, 2000);

或者,如果对话框关闭时有动画,请将 location 赋值放在对话框的 close event 中:

setTimeout(function(){
    $('#wrapper_dialog').dialog('close').on("dialogclose", function() {
        window.location = "?load=parents/communication";
    });                
}, 2000);

那将等到动画完成后才更改位置。

如果您要重定向到另一个页面,则无需关闭对话框,但我想如果需要时间才能从服务器获得响应,这是有道理的。无论如何:

setTimeout(function(){ 
    $('#wrapper_dialog').dialog('close');
    window.location = "?load=parents/communication";
},2000);

您需要将关闭事件处理程序传递给对话框:

setTimeout(function(){
    $('#wrapper_dialog').dialog('close');
    $( "#wrapper_dialog" ).on( "dialogclose", function() {
        window.location = "?load=parents/communication";
    });
}, 2000);

查看 dialog widget documentation