Click another click after 1 second

0

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

I found this solution here . But I would 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><divid="box">
  <button id="button1">Botao 1</button>
  <button id="button2">Botao 2</button>
</div>
    
asked by anonymous 29.11.2017 / 17:05

1 answer

1

You 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><divid="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 execute after the given time.

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

    
29.11.2017 / 17:09