Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delaying a click event with .delay()

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.

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

or Using setTimeout;

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

But didn't work.

like image 932
Talha Avatar asked Feb 04 '13 03:02

Talha


People also ask

Can I delay an onclick event?

No, but your function can set a timer event for whatever delay you want to handle it later.

What is delay () in JavaScript?

Conclusion. setTimeout() is a method that will execute a piece of code after the timer has finished running. let timeoutID = setTimeout(function, delay in milliseconds, argument1, argument2,...); The delay is set in milliseconds and 1,000 milliseconds equals 1 second.

How do you add a delay to a function?

To delay a function call, use setTimeout() function. functionname − The function name for the function to be executed. milliseconds − The number of milliseconds. arg1, arg2, arg3 − These are the arguments passed to the function.

How do you delay a href?

To delay a link with inline javascript, just set your href attribute as href="javascript:setTimeout(()=>{window. location = 'URL' },500);" . When you replace the URL with your link, just make sure it is inside the ' ' .


1 Answers

from the docs 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.

you can use setTimeout to bind click handler after a delay

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);
      });
    });
like image 88
Dakait Avatar answered Sep 30 '22 16:09

Dakait