如何在特定号码后停止循环?

时间:2018-05-31 14:00:35

标签: javascript

我需要在320次后自动点击停止。

怎么做?

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">

2 个答案:

答案 0 :(得分:1)

您需要使用clearInterval函数

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);

答案 1 :(得分:0)

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

a();