jquery .click 函数的基本延迟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2549269/
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
basic delay on jquery .click function
提问by kalpaitch
I have the most basic jquery function of them all, but I couldn't find a way in the documentation to trigger the contents of this click function after say 1500 milliseconds:
我有最基本的 jquery 函数,但是我在文档中找不到在 1500 毫秒后触发这个点击函数内容的方法:
$('.masonryRecall').click(function(){
$('#mainContent').masonry();
});
P.S. just noticed the .delay function jquery 1.4, although, I am using version 1.3. I don't know whether updating this would interfere with any of the other javascript I currently have.
PS 刚刚注意到 .delay 函数 jquery 1.4,不过,我使用的是 1.3 版。我不知道更新这是否会干扰我目前拥有的任何其他 javascript。
回答by Byron Whitlock
You can do it with regular javascript using setTimeout()
.
您可以使用常规的 javascript 来做到这一点setTimeout()
。
$('.masonryRecall').click(function(){
setTimeout("$('#mainContent').masonry()", 1500);
});
回答by David Murdoch
You should generally stay away from string literals in setTimeout/setInterval. Instead use a closure:
您通常应该远离 setTimeout/setInterval 中的字符串文字。而是使用闭包:
setTimeout(function(){ $('#mainContent').masonry(); }, 1500);`
and even better use it like this (note: the outer closure isn't really necessary):
甚至更好地使用它(注意:外部闭包并不是真正必要的):
(function($){
var timeout=null;
$('.masonryRecall').click(function(){
clearTimeout(timeout);
timeout=setTimeout(function(){$('#mainContent').masonry();}, 1500);
});
}(jQuery));