Create js runtime locking dialogs using jQuery

I have an existing web application that uses window.showmodaldialog() to display certain forms and, based on the return value, execute some other Java scripts

For instance:

 var retVal = window.showmodaldialog('...'); if (retVal==XXX) callXXX(); else callYYY(); 

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

I am trying to replace these modal windows with jQuery dialogs, but the problem is that after executing $(...).dialog().open() , JavaScript execution does not wait for the dialog to close.

I know that there are jQuery APIs that allow me to set up a callback function, but for me this involves a lot of changes. In any case, I can pause the execution of JavaScript until the dialog is closed (I still have to execute scripts from the dialog).

+4
source share
2 answers

I’m afraid it’s not possible to β€œpause” javascript until you want it initially. The showModalDialog function does this because it is implemented by the browser.

You can simply fulfill your waiting mechanism, and you will need to make changes (as you said, they are many, but do not have to be complex changes).

Here's what comes to my mind:

  • Use callbacks (for me, best)
  • Use setTimeout and your own mechanism, as gddc responded .
  • Use javascript library for custom threads.

But if you are open to plugins, I just found this jQuery plugin: BlockUI and some others that might help you. And there should be several more plugins that already implement what I mentioned in the above paragraphs.

+1
source

Unfortunately not. You will need to use the callback functions. A "lock" like this in JavaScript is a bad idea because there is only one thread of execution, and JavaScript uses the Event Driven model.

You can do something like this to "wait" for the return value, but it does not "block" another 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(); 

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

+3
source

All Articles