简体   繁体   中英

Pausing the execution of the script using a customize alert box

I have overridden the default alert box with a custom dialog box. But I want the script to be paused until the cancel button on the dialog box is clicked. How to achieve it using javascript or jquery ?

PS : I am trying to make a dialog box which has the same functionality as an alert box.

You can not write blocking code in javascript as it is single threaded and runs on the same thread as the UI, see here: Blocking "wait" function in javascript?

You could do it via callbacks or events that get fired when your custom alert box gets closed.

function CustomAlert(message, callback)
{
    alert(message);
    callback();
}

function CodeWhichGetsBlocked()
{
    DoSomething();
    CustomAlert("continue?", function() {
        DoSomething();
    });
}

I will use dynamic HTML.

var $divAlert = $("<div>Are you sure going to next step?</div>");

$divAlert.dialog({
                autoOpen: true,
        height: "auto",
        width: "auto",
        modal: true,
        buttons: {
            "OK": function(){
                //Your Code/Logic goes here
            },
                    "Cencel": function(){
                        $(this).dialog("close");
                    },
        }
});

you can also customize in temrs of design your dialog just add class.

like,

dialogClass: "MyDialog"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM