jQuery-UI Dialog Widget - Limit number of times allowed to be triggered

113 views Asked by At

Is there a way to limit to the number of times a dialog box is triggered to appear until the page is reloaded? Specifically, I am using the jQuery UI Dialog Widget. I was hoping there was some way of feeding this into the options of the widget - I imagine it would look something like

// div#notice is the name of the dialog element, 
// with style="display:none" by default
$("#notice").dialog({
    dialogClass: "noticeDialog",
        buttons: [{
            text: "OK",
            click: function() {
                $(this).dialog("close");
            }
        }],
        repeat: 1 // A Dialog option equivalent to this does not exist to
                  // my knowledge
 }

The only way I can think of achieving this is for the dialog box to .remove() itself from the DOM inside the 'close' option callback function. Something like:

$(".selector").dialog({
    close: function() {
        $(#"dialogBox").remove();
    }
});

I have a feeling this will not work as hoped, however. What better options are there? If there is a way to set a timeout on the dialog box, that would also be helpful and/or accomplish a similar effect.

1

There are 1 answers

4
Barmar On BEST ANSWER

Do it in the code that triggers the dialog. First, initialize a counter:

var dialog_counter = 0;
var dialog_limit = 5;

Then in the code that displays the dialog, increment and test it:

if (++dialog_counter <= dialog_limit) {
    $("#notice").dialog({
        ...
    });
}