开发者

Create JS execution blocking dialogs using jQuery

开发者 https://www.devze.com 2023-01-12 07:41 出处:网络
I have an existing web application which uses window.showmodaldialog() to show certain forms and based on the return value it executes some other JavaScripts

I have an existing web application which uses window.showmodaldialog() to show certain forms and based on the return value it executes some other JavaScripts

For example:

var retVal = window.showmodaldialog('...');开发者_如何学运维

if (retVal==XXX)
  callXXX();
else
  callYYY();

When showModalDialog() is executed it blocks the JS execution on the main window until the modal is closed.

I am trying to replace these modal Windows with jQuery dialogs but the problem is that once the $(...).dialog().open() is executed the JavaScript execution does not wait for the dialog to be closed.

I know that there are jQuery APIs which allow me to configure a callback function but that involves a lot of changes for me. Is there anyway by which I can pause JavaScript execution until the dialog is closed (I must still be able to execute scripts from the dialog).


Unfortunately, no. You will need to use callback functions of some kind. 'Blocking' like this in JavaScript is a bad idea because there is only a single execution thread and JavaScript uses an Event Driven model.

You could do something like this to "wait" for the return value, but this doesn't "block" other execution at all:

var myReturn, 
myInterval = setInterval(function () {
  if (myReturn != undefined) {
    clearInterval(myInterval);
    // Rest of processing code here.
  }
}, 50);

$('myContainer').dialog(close: function () {
  myReturn = 'Dialog Closed';
}).open();

Attempting to "block" or "pause" JavaScript execution should be avoided - the language is simply not designed for it.


I'm afraid there is no way to "pause" javascript execution until you want natively.. The showModalDialog function do that because is implemented by the browser.

You can just do your "waiting" mechanism by hand, and You'd need to do the changes (as you said they are a lot, but shouldn't be complex changes).

These are the things that comes to my mind now:

  • Make use of callbacks ( to me, the best by far )
  • Use setTimeout and your own mechanism, as g.d.d.c answered .
  • Use some user threads javascript library.

But if you're open to plugins, I've just found this jQuery plugin: BlockUI , and some others that maybe help you. And out there must be a few other plugins that already implement what I mentioned in the points above.

0

精彩评论

暂无评论...
验证码 换一张
取 消