-1

I have this jquery code to press buttons for me, it will press it every second - some page just loads forever until that button appears. This code is successful:

(function($){
    setInterval(function(){
        $('.play-button').click();
    }, 1000);

})(jQuery)

Now I want one step more: I want it press it only once. (still, this button only shows after a random long loading time.)

user2650501
  • 119
  • 4
  • 11
  • Does this answer your question? [Execute the setInterval function without delay the first time](https://stackoverflow.com/questions/6685396/execute-the-setinterval-function-without-delay-the-first-time) – Farhad Shekari Feb 09 '20 at 11:23

2 Answers2

1

Use a statement to make it to run just once

let clicked = false;

(function($) {
  setInterval(function() {
  !clicked ? $('.play-button').click() : null
  }, 1000);

})(jQuery)

$('.play-button').click(function() {
  console.log('clicked')
  clicked = !clicked
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="play-button">play button</button>
Pedram
  • 15,766
  • 10
  • 44
  • 73
1

Use setTimeout:

(function($){
    setTimeout(function(){
        $('.play-button').click();
        $('.play-button').click(function(){return;});
    }, 1000);
})(jQuery)
Ritesh Khandekar
  • 3,885
  • 3
  • 15
  • 30