【问题标题】:How to stop loop after specific number?如何在特定数字后停止循环?
【发布时间】:2018-05-31 14:00:35
【问题描述】:

我需要自动点击在 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">

【问题讨论】:

标签: javascript


【解决方案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);

【讨论】:

  • 太棒了,谢谢迪伦!:)
【解决方案2】:

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

a();

【讨论】:

    猜你喜欢
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    • 2019-10-17
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    相关资源
    最近更新 更多