views:

160

answers:

2

How can I destroy a dialog box after a certaing amount of seconds?????

This is my code:

<script type="text/javascript">
 $(function() {
  $(".dialog-message").dialog({
   modal: true,
   buttons: {
    Ok: function() {
     $(this).dialog('close');
    }
   }
  });
 });

 </script>
+2  A: 
$(function() {
var dialog = $(".dialog-message").dialog({
    modal: true,
    buttons: {
        Ok: function() {
            $(this).dialog('close');
        }
    }
});

setTimeout(function(){
    dialog.dialog('destroy');
},5000); // 5 seconds
});
PetersenDidIt
Above wont work as `dialog` wont be defined in the anonymous function passed to setTimeout
azatoth
+1 - This works fine.
patrick dw
Works flawlessly
@azatoth - The function is referencing a variable in the same scope. It will utilize whatever value that variable holds at the point in time when it runs. Would be the same as if you removed the `setTimeout()`, and only had the function.
patrick dw
@patrick: Hehe, oops. The indentation fooled me; I though the `$(function(){});` ended directly after the creation of the dialog.
azatoth
A: 
 function destroyDialog() {
      $(".dialog-message.").dialog("destroy");
 }

 setTimeout("destroyDialog()", 1000);

This does it after 1 second, 1000 milliseconds...

Mike
I would recommend to change the setTimeout here to setTimeout(destroyDialog, 1000);
azatoth
You added a trailing `.` to your selector, and your function will be called against the global namespace, so `destroyDialog()` would need to be defined globally.
patrick dw