Reorganizing wait algorithm in HtmlDialog.

Now is more correctly aborting the wait if a new (cascading) dialog is
detected. Fixes issue #6744.