58

How to click a button every second using JavaScript?

2
  • 5
    but... why? maybe some context would help? ie there could be a better way to achieve what you are trying to do... rather than 'click'ing a button every second... Commented Dec 23, 2010 at 4:54
  • 2
    Because decisionproblem.com/paperclips Commented Jun 15, 2019 at 8:10

6 Answers 6

127
setInterval(function () {document.getElementById("myButtonId").click();}, 1000);
Sign up to request clarification or add additional context in comments.

4 Comments

is that actually emulating a button click or just calling the event listener for myButtonId??
Can you do this by targeting a button by its class name instead?
@Kelseydh Yes you could, but remember getElementsByClassName() will return an array of elements and not just one. Therefore you would need to iterate over the array force the click execution. also note this method is present in I all current browsers but not available in some older browsers. here is some reference. developer.mozilla.org/en-US/docs/Web/API/Document/…
@Kelseydh. I also answered this for a pure JAVASCRIPT impementation. JQuery provides a slicker interface to handle this.
7

This will give you some control over the clicking, and looks tidy

<script>
var timeOut = 0;
function onClick(but)
{
    //code
    clearTimeout(timeOut);
    timeOut = setTimeout(function (){onClick(but)},1000);
}
</script>
<button onclick="onClick(this)">Start clicking</button>

3 Comments

Is there any reason this one clears and recreates its own timer every time it's called, as opposed to using interval = setInterval(...) which repeats on its own, and using clearInterval(interval) when necessary?
Each time you click it, it starts another loop, just figured I would halt it inside the loop so you could do a condition check before starting it again. Also, this is using setTimeout which is just to show an alternate method to setInterval
Alright, sure, this is a decent alternative depending on the effect someone wants. +1
5
document.getElementById('youridhere').click()

1 Comment

This only partially answers the question, the repeating part of the question is still oustanding.
2

This would work

setInterval(function(){$("#myButtonId").click();}, 1000);

1 Comment

Adding some description would help.
2

You can use

setInterval(function(){ 
    document.getElementById("yourbutton").click();
}, 1000);

Comments

0

this will work ,simple and easy

<form method="POST">
  <input  type="submit" onclick="myFunction()" class="save" value="send" name="send" id="send" style="width:20%;">
</form>
<script language ="javascript" >
  function myFunction() {
    setInterval(function() {
      document.getElementById("send").click();
    }, 10000);    
  }
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.