jQuery UI对话框使用setTimeout自动关闭 [英] jQuery UI Dialog Auto-Close using setTimeout
问题描述
我正在尝试让对话框在打开后三秒钟自动关闭。我尝试了以下方法:
I'm trying to have my dialog auto-close three seconds after opening. I've tried the following methods:
setTimeout($("#mydialog").dialog('close'), 3000);
这里是上下文:
$("#acknowledged-dialog").dialog({
height: 140,
modal: true
});
setTimeout($("#acknowledged-dialog").dialog('close'), 3000);
但是使用这种方法,它甚至都没有显示!我猜测close方法在页面显示后立即被调用。日志显示没有错误。
But with this method, it doesn't even show! I'm guessing the the close method is getting called immediately after it gets shown on the page. The log shows no errors.
我也尝试绑定到dialogopen事件:
I've also tried binding to the dialogopen event:
$("#acknowledged-dialog").bind('dialogopen', function(event, ui) {
setTimeout($(this).dialog('close'), 3000);
});
$("#acknowledged-dialog").dialog({
height: 140,
modal: true
});
对话框显示,但不会自动关闭。这里的日志也没有错误。
The dialog shows, but does not auto-close. No error in the logs here either.
我是否无法在setTimeout中的$参数中使用'this'?
Am I not able to use 'this' in the argument for $ in setTimeout?
推荐答案
setTimeout在3秒后调用$(#mydialog)。对话框(关闭)的返回值。你想把整个东西扔成一个字符串,它应该工作得很好。另外,我不认为你想在初始化对话框之前绑定'dialogopen'。下面应该可以正常工作:
setTimeout is calling on the the return value of $("#mydialog").dialog("close") after 3 seconds. you want to throw the whole thing as a string, and it should work just fine. Also, I don't think you want to bind 'dialogopen' before you initialize the dialog. Below should work just fine:
$("#acknowledged-dialog").dialog({
height: 140,
modal: true,
open: function(event, ui){
setTimeout("$('#acknowledged-dialog').dialog('close')",3000);
}
});
这篇关于jQuery UI对话框使用setTimeout自动关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!