jQuery 使用 .delay() 延迟点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14679875/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:50:42  来源:igfitidea点击:

Delaying a click event with .delay()

jquerydelaytimedelay

提问by Talha

I have a button that gets triggered by the click of another button. I want to delay click of the second button for two seconds. I used .delay() but it didn't work.

我有一个通过单击另一个按钮触发的按钮。我想延迟点击第二个按钮两秒钟。我使用了 .delay() 但它没有用。

jq(function() {
      jq('a.box').click(function() {
         jq(this).closest('.button').find('.add_this').delay(2000).click();
      })
    });

or Using setTimeout;

或使用 setTimeout;

jq(function() {
      jq('a.box').click(function() {
      setTimeout(function(){
         jq(this).closest('.button').find('.add_this').click();
      },800);
      });
    });

But didn't work.

但是没有用。

回答by Dakait

from the docs http://api.jquery.com/delay/

来自文档http://api.jquery.com/delay/

The .delay() method is best for delaying between queued jQuery effects. Because it is limited—it doesn't, for example, offer a way to cancel the delay—.delay() is not a replacement for JavaScript's native setTimeout function, which may be more appropriate for certain use cases.

.delay() 方法最适合在排队的 jQuery 效果之间延迟。因为它是有限的——例如,它不提供取消延迟的方法——.delay() 不能替代 JavaScript 的原生 setTimeout 函数,后者可能更适合某些用例。

you can use setTimeoutto bind click handler after a delay

您可以setTimeout在延迟后使用绑定点击处理程序

setTimeout(function(){

jq('a.box').closest('.button').find('.add_this').click();
},2000);

EDIT

编辑

jq(function() {
      jq('a.kklike-box').click(function() {
      $this = $(this);
      setTimeout(function(){
         $this.closest('.deal_buttons').find('.add_this').click();
      },800);
      });
    });