代码运行1000次后结束Javascript脚本

时间:2018-08-17 13:47:49

标签: javascript console

我想知道如何在Javascript脚本运行1000次后停止。任何建议表示赞赏。

这是我的代码:

function run(){
  var first = document.querySelector(".stats-label");
  var next = document.querySelector(".shot-nav-next");

function simulateClick (elem) {
  var evt = new MouseEvent('click', {
    bubbles: true,
    cancelable: true,
    view: window 
  });
  var canceled = !elem.dispatchEvent(evt);
  };

  if (first-pic.innerHTML == "View"){
    simulateClick(first);
    simulateClick(next);
  }
  else {
    simulateClick(next);
  }
}

setInterval((run),20000);

void 0;

1 个答案:

答案 0 :(得分:2)

是这样吗?

var counter = 0;

function run(){
  var first = document.querySelector(".stats-label");
  var next = document.querySelector(".shot-nav-next");
      

  if(counter < 1000){
    if (first-pic.innerHTML == "View"){
      simulateClick(first);
      simulateClick(next);
    }
    else {
       simulateClick(next);
    }
   }
   counter++;
}


function simulateClick (elem) {
   var evt = new MouseEvent('click', {
      bubbles: true,
      cancelable: true,
      view: window 
    });
    var canceled = !elem.dispatchEvent(evt);
 };

 setInterval((run),20000);