如何使用 jquery 工具的叠加延迟 .trigger('click')
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4414153/
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
How to delay .trigger('click') with overlay from jquery tools
提问by Boris Goryachev
I am using Jquery tools, overlay effect and want to close it, if JSON response is OK, but i want to do it with a delay.
我正在使用 Jquery 工具,叠加效果并想关闭它,如果 JSON 响应没问题,但我想延迟完成。
$.ajax({
//bla bla
success: function(data){
var obj = jQuery.parseJSON(data);
if (obj.status=='OK')
{
$('#status').text('bla bla');
jQuery('.close').trigger('click');
}
else
{
$('#status').text('bla bla');
}
}
});
so this - jQuery('.close').trigger('click'); must be executed after some time. Any ideas?
所以这个 - jQuery('.close').trigger('click'); 必须在一段时间后执行。有任何想法吗?
回答by David Tang
setTimeout()
is a native JavaScript function designed for this purpose.
setTimeout()
是为此目的而设计的原生 JavaScript 函数。
setTimeout(function () {
jQuery('.close').trigger('click');
}, 1000);
The last number there is the delay time in milliseconds.
最后一个数字是以毫秒为单位的延迟时间。
回答by TelegramSam
use setTimeout:
使用 setTimeout:
delay here is 1 second (1000 ms)
这里的延迟是 1 秒(1000 毫秒)
$.ajax({
//bla bla
success: function(data){
var obj = jQuery.parseJSON(data);
if (obj.status =='OK')
{
$('#status').text('bla bla');
setTimeout(function(){jQuery('.close').trigger('click');},1000);
}
else
{
$('#status').text('bla bla');
}
}
});
回答by Lex
Not tested.
未测试。
jQuery('.close').delay(500).trigger('click');