javascript jQuery 添加/删除类之间的时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7550365/
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
Time between jQuery add/remove class
提问by EnexoOnoma
How can I set a timer, 10 sec in between this?
我怎样才能设置一个计时器,在这之间间隔 10 秒?
addClass('loading').removeClass('loading')
This is the full code
这是完整的代码
$("#loadmore").click(function() {
cap += 10;
}).bind('click', loadfeed).addClass('loading').removeClass('loading');
Thank you.
谢谢你。
回答by jhchen
Use setTimeout. Also not sure why you are binding to click twice in two different ways... so with those two changes it'd look something like this:
使用setTimeout。也不确定为什么你要以两种不同的方式绑定两次点击......所以通过这两个更改,它看起来像这样:
$("#loadmore").click(function() {
cap += 10;
loadfeed();
$(this).addClass("loading");
that = this
setTimeout(function() {
$(that).removeClass('loading');
}, 10000)
});
回答by Hristo
You can use the jQUery delay() methodand create a new queueitem to do the act of removing the class.
您可以使用jQUEry delay() 方法并创建一个新的队列项来执行删除类的操作。
$("#loadmore").click(function () {
cap += 10;
loadfeed();
}).addClass("loading").delay(10000).queue(function(){
$(this).removeClass("loading");
$(this).dequeue();
});
If you don't like this, the setTimeout()
solution that @jcmoney provided is awesome.
如果您不喜欢这个,setTimeout()
@jcmoney 提供的解决方案很棒。
回答by bcm
Using this is a lot better if you want jQuery in a single line:
如果你想在一行中使用 jQuery,那么使用它会好得多: