mplungjan
mplungjan

Reputation: 178026

Can delay() be used with jQuery UI dialog.open()?

I have this code inline in document ready

if($.cookie('form_seen') == null) { 
  $("#dialog_form").dialog("open");
}

I know how to use setTimeout like How to Delay automatic opening of Modal Dialog box window in JQuery 1.5.x? so no need to post a setTimeout example.

I wondered for my personal education, what the correct syntax would be if I wanted to use .delay instead of setTimeout

if($.cookie('form_seen') == null) { 
  $("#dialog_form").delay(5000).dialog("open");
}

or similar

In this case it is not obvious what I would gain since I would have to wrap $("#dialog_form").dialog("open"); in the function called by setTimeout so no need for closures or finding $(this) back, but in other situations I can imagine chaining being smarter. Comments to pros and cons are very welcome

Upvotes: 2

Views: 6950

Answers (1)

Shef
Shef

Reputation: 45589

if($.cookie('form_seen') == null) { 
  $("#dialog_form")
    .delay(5000)
    .queue(function(next){
        $(this).dialog("open");
        next(); // take this function out of queue a.k.a dequeue a.k.a notify done
                // so the next function on the queue continues execution...
    })
}

OR

if($.cookie('form_seen') == null) { 
  $("#dialog_form")
    .delay(5000)
    .queue(function(){
        $(this)
            .dialog("open")
            .dequeue(); // take this function out of queue a.k.a dequeue a.k.a notify done
                        // so the next function on the queue continues execution...
    })
}

Upvotes: 9

Related Questions