javascriptbootstrap-dialog

Bootstrap dialog rendering postpode


I have come across a very bizarre behaviour of Bootstrap Dialog For some reason, in the foo function provided below, the Dialog box does not show up at once. The rendering is delayed to the moment of reaching the line $.get(.... Any ideas why this could happen ?

function = foo()
{
   $rows.each(function (i, row) 
   {
       var $row = $(row);
       if (something_is_wrong()) 
       {
           alert_error('Something is wrong', $form, '');
           return;
       }
       // Some other code           
    });
    // The Bootstrap modal dialog shows up when reaching the point below !!!
    $.get('/sending_order_notification/' + legal_entity_own_id, function(response)
    {
        BootstrapDialog.show({ ...
        // ...
     });
}


function alert_error(message, $current_form, function_name)
{
    if ($current_form != undefined)
        $current_form.modal('hide');

    BootstrapDialog.show(
    {
        type: BootstrapDialog.TYPE_DANGER,
        title: 'Ошибка',
        message: message,
        draggable: true,
        buttons: [{
            label: 'OK',
            action: function(dialogItself) {
                dialogItself.close();
                if (function_name != undefined)
                    $.post('/send_error_report/', function_name);
            }
        }]
    });  
}

UPDATE Inspired by Maximus' anwser, I opted for the following work-around which worked for me. And yet this is not a clean solution, because I have to continue the loop even if it becomes senseless.

function = foo()
    {
       var bad_condition_flg = false;
       $rows.each(function (i, row) 
       {
           var $row = $(row);
           if (something_is_wrong()) 
           {
               bad_condition_flg = true;
           }
           // Some other code           
        });
        if (bad_condition_flg);
        {
           alert_errr(...);  
           return;
         }
     }

Solution

  • In order for the dialog to be shown, the browser has to perform repaint. Repaint is only possible when there's nothing in the call stack. So the dialog will be shown only after foo has finished executing. It's a bit different when you use debugger, because sometimes stopping at breakpoints gives a browser time for repaint and the dialog might be shown before the call stack is empty.