Diskdrive
Diskdrive

Reputation: 18835

How do I get a JQuery statement to wait until it's completed before continuing?>

Firstly, I'm new at Javascript / Jquery so it might be a stupid question...

I'm using the dialog box that is in the JQuery UI collection. I have a button that when clicked, it either shows a confirm box or an alert box. My code is like below...

            function checkfn() {

            if (document.getElementById('<%=HomeInstSelected.ClientID%>').value == 'False') {

                var dialogResult = false;

                $("#dialog-confirm").dialog({
                    resizable: false,
                    height: 140,
                    modal: true,
                    buttons: {
                        Continue: function () {
                            dialogResult = true;
                            $(this).dialog("close");

                        },
                        Cancel: function () {
                            $(this).dialog("close").slideUp();
                        }
                    }
                });

                // alert just debug code!
                alert(dialogResult);
                return dialogResult;

            }
            else {
                $("#dialog-HomeInstitutionPrompt").dialog({
                    height: 140,
                    modal: true
                });

            } 
        }

My problem is in the confirm part, it seems the confirm box is not waiting for me to hit Continue or Cancel - it's just going straight to the alert part and returning dialogResult = false.

Is it possible to halt execution of until after I've run the $('#dialog-confirm') command?

Alternatively, is it possible to return true for the checkfn() function, in the Continue function? That way, I will not need a dialogResult var at all?

Upvotes: 1

Views: 13521

Answers (4)

SharpC
SharpC

Reputation: 7464

I had a similar frustrating problem where the jQuery dialog box would submit a form before the user had a chance to choose Yes or No.

Example HTML:

<form id="myForm" method="POST" action="/Where/To/Go">
    <input type="submit" value="Submit" onclick="submitForm()"/>
</form>

And jQuery:

function submitForm() {
    $('<div title="Submit Form>Are you sure you wish to submit the form?</div>').dialog({
        modal: true,
        buttons: [
            {
                text: "Yes",
                click: function () {
                    $('#myForm').submit();
                    $(this).dialog("close");
                }
            }, {
                text: "No",
                click: function () {
                    $(this).dialog("close");
                }
            }
        ]
    });
}

To fix the issue I changed the button from a submit type to a button type which stopped it submitting and triggering the jQuery dialog at the same time:

    <input type="button" value="Submit" onclick="submitForm()"/>

Upvotes: 0

Brian
Brian

Reputation: 38035

I haven't used the .dialog in jQuery UI before, but, in general with jQuery, functions are run asynchronously. What this means is that you need to put the code that you want run in the Continue function.

One possibility is to send a success/cancel function to checkfn that you would call from the Continue and Cancel functions.

function checkfn(success, cancel) {

if (document.getElementById('<%=HomeInstSelected.ClientID%>').value == 'False') {

    var dialogResult = false;

    $("#dialog-confirm").dialog({
        resizable: false,
        height: 140,
        modal: true,
        buttons: {
            Continue: function () {
                if(success) success();
                $(this).dialog("close");

            },
            Cancel: function () {
                if(cancel) cancel();
                $(this).dialog("close").slideUp();
            }
        }
    });
}

You can call the function like this:

checkfn(function () {
    alert('success!');
}, function () {
    alert('failure!');
});

Upvotes: 4

Greg Guida
Greg Guida

Reputation: 7512

This is something I struggled with when I started too, The code doesn't run as it reads on the page. When you call the dialog function it executes asynchronously. The continue and cancel functions are bound to the click actions on the buttons, meanwhile the code below the dialog function runs without waiting for the events.

Long story short the result needs to happen in the cancel and continue callbacks.

Problem is you're trying to return a boolean value when you should really pass the resulting functions in instead. Alot of things in jquery and javascript in general work that way. Thankfully the language provides the ability to program in this way.

    function checkfn( resultfn(boolval) ) {

    if (document.getElementById('<%=HomeInstSelected.ClientID%>').value == 'False') {

        var dialogResult = false;

        $("#dialog-confirm").dialog({
            resizable: false,
            height: 140,
            modal: true,
            buttons: {
                Continue: function () {
                    resultfn.call( this, true );
                    $(this).dialog("close");

                },
                Cancel: function () {
                    resultfn.call( this, false );
                    $(this).dialog("close").slideUp();
                }
            }
        });

}

Put the if statement in "resultfn"

Upvotes: 1

SadullahCeran
SadullahCeran

Reputation: 2415

Just put everything you want to do inside "Continue" and "Cancel" button definitions. So, you will not need a dialogResult and alert will hit when it is needed.

if (document.getElementById('<%=HomeInstSelected.ClientID%>').value == 'False') {

                var dialogResult = false;

                $("#dialog-confirm").dialog({
                    resizable: false,
                    height: 140,
                    modal: true,
                    buttons: {
                        Continue: function () {
                            alert('Dialog result is true. I can do whatever I want here');
                            $(this).dialog("close");

                        },
                        Cancel: function () {
                            alert('Cancel is clicked. I should go on my life');
                            $(this).dialog("close").slideUp();
                        }
                    }
                });
            }
            else {
                $("#dialog-HomeInstitutionPrompt").dialog({
                    height: 140,
                    modal: true
                });
            } 

-- You can't return a value for your function, because after initialization of the function, the code goes on. You need to fill Continue button definition.

Upvotes: 1

Related Questions