Make a click fire another click after 1 second

Asked

Viewed 544 times

0

I would like to know how to click on a button to fire another click after 1 second.

I found this solution here. But I would like to add this delay of 1 second.

Code:

$("#button1").click(function(){
  $("#button2").trigger('click');
});

$("#button2").click(function(){
  $("#box").append('<p>Botao 2 clicado</p>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="box">
  <button id="button1">Botao 1</button>
  <button id="button2">Botao 2</button>
</div>

1 answer

1


Can add a setTimeout:

$("#button1").click(function(){
  setTimeout(function() {
    $("#button2").trigger('click');
  }, 1000);
});

$("#button2").click(function(){
  $("#box").append('<p>Botao 2 clicado</p>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="box">
  <button id="button1">Botao 1</button>
  <button id="button2">Botao 2</button>
</div>

The first parameter is a function with the commands you want to run after the given time.

The second parameter is the time in milliseconds (in case 1000ms = 1s).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.