I'm trying to extend alert function and let it close after a timeout.
I'm getting this error when close timeout expire:
cannot call methods on dialog prior to initialization; attempted to call method 'close'
The code of extension is:
$.extend({ alert: function (message, title) {
$("<div></div>").dialog( {
buttons: { "Ok": function () { $(this).dialog("close"); } },
close: function (event, ui) { $(this).remove(); },
resizable: false,
title: title,
modal: true,
open: function(event, ui) {
setTimeout(function(){
$(this).dialog('close'); <-- Error is here
}, 3000);
}
}).text(message);
}
});
And the call method is:
$.alert('Alert Message Test');
Next, after 3000 ms, the dialog should close. What's wrong?
UPDATE
Thanks to Kevin, now it works! Yes, it's the same problem of your reported answer but... it's so hard to find. This question is connected to the JS error.
$.extend({ alert: function (message, title) {
$("<div></div>").dialog( {
buttons: { "Ok": function () { $(this).dialog("close"); } },
close: function (event, ui) { $(this).remove(); },
resizable: false,
title: title,
modal: true,
open: function(event, ui) {
var that = this; <-- This is the solution
setTimeout(function(){
$(that).dialog('close');
}, 3000);
}
}).text(message);
}
});