Javascript jQuery UI 对话框使用 setTimeout 自动关闭
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7954953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery UI Dialog Auto-Close using setTimeout
提问by daniel0mullins
I'm trying to have my dialog auto-close three seconds after opening. I've tried the following methods:
我试图让我的对话框在打开后三秒钟自动关闭。我尝试了以下方法:
setTimeout($("#mydialog").dialog('close'), 3000);
Here it is in context:
这是在上下文中:
$("#acknowledged-dialog").dialog({
height: 140,
modal: true
});
setTimeout($("#acknowledged-dialog").dialog('close'), 3000);
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.
但是用这种方法,它甚至不显示!我猜 close 方法在页面上显示后会立即被调用。日志显示没有错误。
I've also tried binding to the dialogopen event:
我也尝试绑定到 dialogopen 事件:
$("#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.
对话框显示,但不会自动关闭。这里的日志中也没有错误。
Am I not able to use 'this' in the argument for $ in setTimeout?
我不能在 setTimeout 的 $ 参数中使用“this”吗?
回答by DefyGravity
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:
setTimeout 在 3 秒后调用 $("#mydialog").dialog("close") 的返回值。你想把整个东西作为一个字符串抛出,它应该可以正常工作。另外,我认为您不想在初始化对话框之前绑定“dialogopen”。下面应该工作得很好:
$("#acknowledged-dialog").dialog({
height: 140,
modal: true,
open: function(event, ui){
setTimeout("$('#acknowledged-dialog').dialog('close')",3000);
}
});
回答by airportyh
I wrote an articlespecifically for the problem you are experiencing. Please read that.
我专门针对您遇到的问题写了一篇文章。请阅读那个。
In short, you want to wrap $("#mydialog").dialog('close')
with an inline function everywhere you want it executed as a result of a delay or a triggered event.
简而言之,您希望在$("#mydialog").dialog('close')
任何您希望它因延迟或触发事件而执行的地方使用内联函数进行包装。
setTimeout(function(){
$("#mydialog").dialog('close')
}, 3000);
The dialog doesn't even show because you closed it as soon as you opened it in each case.
该对话框甚至不显示,因为您在每种情况下打开它后立即关闭它。