-3

I need the auto click to stop after 320 times.

How to do it?

var = "320";
// i need the loop to stop after 320 times.
var button = document.getElementById("jsonp2");
setInterval(function() {
  button.click();
}, 10000);
<input type="button" id="jsonp2" href="javascript:void(0)" onclick="javascript:alert('button autoclicked');" class="btn refreshListButton" title="Refresh">
Ivar
  • 6,138
  • 12
  • 49
  • 61
relomi
  • 1
  • 2
    Possible duplicate of [how to make a setInterval stop after some time or after a number of actions?](https://stackoverflow.com/questions/9136261/how-to-make-a-setinterval-stop-after-some-time-or-after-a-number-of-actions) – Ivar May 31 '18 at 14:02

2 Answers2

1

You need to use the clearInterval function

http://jsfiddle.net/pdmafjpa/71/

var iterations = 5;
var count = 0;

var button = document.getElementById("jsonp2");
var myInterval = setInterval(function(){ 
    if (count >= iterations) {
    clearInterval(myInterval);
  } else {
    count++;
        button.click();  
  }
}, 2000);
Dylan
  • 1,681
  • 11
  • 16
0

var i=1;
function a(){
if(i<320)
{
console.log(i);
setTimeout(a,1000);
i++;
}}

a();