How to stop a script when using jQuery Dialog?

I am sure that this has been asked many times. the last one I saw was in December 2012. But I would like to ask if anyone has a fix / best workaround for this problem.

I am using jQueryUI Dialog and I would like to ask if it is possible to stop the execution of the script when a dialog appears and the user has not yet selected an answer. Like javascript confirm() .

Here is what I still have:

JQuery

 $("#GlobalDialogBox").dialog({ modal: true, buttons: { "Yes": function () { callbackFunctionTrue(); $(this).dialog('close'); }, "No": function () { callbackFunctionFalse(); $(this).dialog('close'); } } }); alert("This should fire after answering the dialog box."); //but this fires as soon as the dialog pops up.. :( 

alert is a common part of a script that runs after a response to a dialog box, regardless of the response. According to my fix, I just inserted alert into callbackFunctionTrue() and callbackFunctionFlase() , and it does the job.

But what if I do not want to do this? How can I stop the script when a dialog appears so that I can freely add my codes below the dialog script? Does anyone have a better solution? Any help would be appreciated! Thank you in advance!:]

+6
source share
5 answers

When the dialog closes, it causes the event to close. You can do this by adding an event listener to your dialog:

 $("#GlobalDialogBox").on( "dialogclose", function( event, ui ) { alert("something"); }); 

or more sequentially:

 $("#GlobalDialogBox").dialog({ modal: true, buttons: { "Yes": function () { callbackFunctionTrue(); $(this).dialog('close'); }, "No": function () { callbackFunctionFalse(); $(this).dialog('close'); } }, close: function(event, ui){ alert("something"); } }); 
+3
source

You cannot pause script execution arbitrarily. Callback functions are the best solution in this case, therefore, under the buttons in the dialog box, add a close event handler and place your warning and any other code that you want to execute after closing the dialog box.

+1
source
 function dialog_confirm(){ $( "#dialog-confirm" ).dialog({ //autoOpen: false, resizable: false, height:140, modal: true, buttons: { "Delete all items": function() { $( this ).dialog( "close" ); dialog_confirm_callback(true); }, Cancel: function() { $( this ).dialog( "close" ); dialog_confirm_callback(false); } } }); } function dialog_confirm_callback(value) { if (value) { alert("Confirmed"); } else { alert("Rejected"); } } 
+1
source

Not sure why you don't want to do this. What you implemented is the same as in the same situation when I implemented it, and everything turned out just fine. Launch the dialog in your own JavaScript function and run the yes / no functions based on which element is clicked. Warning not required.

0
source

you can use return to stop the function.

-2
source

Source: https://habr.com/ru/post/947176/


All Articles