单击时,Jquery按钮对话框不会立即关闭

时间:2017-06-14 22:39:40

标签: javascript jquery

我有一个名为“全部显示”的按钮,当用户点击它时,它会弹出一个窗口来确认用户是否想显示所有实体。我在这个对话框中使用Jquery,我有:

function showAll() {
    $("#" + showAllDiv).show();
    $("#" + showAllDiv).dialog( {
         title: XXX,
         height: XXX,
         width: XXX,
         modal : true,
         buttons: {
             "Yes": function () {
                  $(this).dialog("close");
                  addLoadingFn(); // it will show a "please wait..." dialog
                  heavyDutyWorkFn();  //a function to get all data and show on the pop up HTML window;
                  deleteLoadingFn(); // it will close the "please wait..." dialog
             },
             Cancel: function() {
                  $(this).dialog("close");
             }
         }
    });
    $("#" + showAllDiv).html(You sure to show all?)
 }

问题是,当我单击是时,对话框不会立即关闭。它仍然会停留一段时间,直到“heavyDutyWorkFn()”完成它的工作。在另一个世界中,点击“是”后,$(this).dialog(“close”)将不会关闭,直到heavyDutyWorkFn()完成显示所有数据。

有人知道发生了什么吗?任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

您可以与heavyDutyWorkFn()异步运行setTimeout(),因此不会延迟脚本的其余部分。

function showAll() {
    $("#" + showAllDiv).dialog( {
         title: XXX,
         height: XXX,
         width: XXX,
         modal : true,
         buttons: {
             "Yes": function () {
                  $(this).dialog("close");
                  addLoadingFn(); // it will show a "please wait..." dialog
                  setTimeout(function() {
                    heavyDutyWorkFn();  //a function to get all data and show on the pop up HTML window;
                    deleteLoadingFn(); // it will close the "please wait..." dialog
                  }, 1);
             },
             Cancel: function() {
                  $(this).dialog("close");
             }
         }
    }).html("You sure to show all?")
 }

顺便说一句,没有必要致电.show(),因为.dialog()会自动显示对话框(除非您使用autoOpen: false)。