0

Ok, so I have a sort of obscure question. I know what the problem is and the solution, but I don't understand why.

There is one method that steps through some prep work, validation, saving and cleaning up. We have this logic in a few different instances. I was hoping to get some insight as to why the first method works but the second method displays a broken image for the "doneMessage" Is there a reference that's lost or something like that???

I imagine I'll get the questions about why the UI is being blocked right before the page is refreshed. Before this method is called the UI is blocked with a please wait message which works perfectly. This is just a visual confirmation to the user that the save did happen before refreshing the page.

The code for the AjaxCall is just a simple JSON AJAX call. I can post the code if needed but I don't think it's really relevant to the question.

This works with the $.blockUI call in the success handler of the AJAX call

    function Step(stepNumber) {
        switch (stepNumber) {
            case 1:
                ajaxParameters = "blah:'" + blah + "', yada:" + yada;
                setTimeout(function () { Step(2) }, 50);
                break;
            case 2:
                AjaxCall("FormStyle.aspx", "Validate", ajaxParameters, function () {
                    setTimeout(function () { Step(3) }, 50);
                });
                break;
            case 3:
                AjaxCall("FormStyle.aspx", "Save", ajaxParameters, function () {
                    $.blockUI({ message: doneMessage, css: { padding: 5} });
                    setTimeout(function () { RefreshCurrentPage() }, 50);
                });
                break;
        }
    }

This however, does not. It displays a broken image:

    function Step(stepNumber) {
        switch (stepNumber) {
            case 1:
                ajaxParameters = "blah:'" + blah + "', yada:" + yada;
                setTimeout(function () { Step(2) }, 50);
                break;
            case 2:
                AjaxCall("FormStyle.aspx", "Validate", ajaxParameters, function () {
                    setTimeout(function () { Step(3) }, 50);
                });
                break;
            case 3:
                AjaxCall("FormStyle.aspx", "Save", ajaxParameters, function () {
                    setTimeout(function () { Step(4) }, 50);
                });
                break;
            case 4:
                $.blockUI({ message: doneMessage, css: { padding: 5} });
                RefreshCurrentPage()
                break;
        }
    }
Justin williams
  • 574
  • 1
  • 8
  • 26

1 Answers1

0

Make sure you set the ajax parameter async: false. This will ensure your code will not execute anything else prior to this code finishing.

CR41G14
  • 5,464
  • 5
  • 43
  • 64
  • I don't think it's a matter of things being called out of sequence. The next action only happens on the success of the AJAX call. Whether it calls Step(4) or the $.blockUI method directly, both would happen at the same time. – Justin williams Dec 12 '12 at 21:05