How to click a button every second using JavaScript?
Asked
Active
Viewed 1.7e+01k times
58
-
5but... 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... – Damien-Wright Dec 23 '10 at 04:54
-
2Because http://www.decisionproblem.com/paperclips/ – Darvanen Jun 15 '19 at 08:10
6 Answers
127
setInterval(function () {document.getElementById("myButtonId").click();}, 1000);

John Hartsock
- 85,422
- 23
- 131
- 146
-
is that actually emulating a button click or just calling the event listener for `myButtonId`?? – Damien-Wright Dec 23 '10 at 04:56
-
Can you do this by targeting a button by its class name instead? – Kelsey Hannan Feb 10 '17 at 07:47
-
2@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. https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName – John Hartsock Feb 16 '17 at 21:07
-
2@Kelseydh. I also answered this for a pure JAVASCRIPT impementation. JQuery provides a slicker interface to handle this. – John Hartsock Feb 16 '17 at 21:09
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>

Isaac
- 11,409
- 5
- 33
- 45
-
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? – doppelgreener May 27 '13 at 23:20
-
2Each 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` – Isaac May 27 '13 at 23:24
-
Alright, sure, this is a decent alternative depending on the effect someone wants. +1 – doppelgreener May 27 '13 at 23:27
5
document.getElementById('youridhere').click()

KonaRin
- 99
- 1
- 2
-
5This only partially answers the question, the repeating part of the question is still oustanding. – greedybuddha May 09 '13 at 00:08
2
You can use
setInterval(function(){
document.getElementById("yourbutton").click();
}, 1000);

Nikolay Mihaylov
- 3,868
- 8
- 27
- 32

Footer
- 129
- 4
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>

Moritz Ringler
- 9,772
- 9
- 21
- 34

nikunj
- 33
- 1
- 9