Siddhesh Patil Siddhesh Patil - 25 days ago 16
Javascript Question

clearInterval doesn't terminate

This is a auto tag script I am working on. It can be implemented on a 9gag post.(http://9gag.com/gag/*)

var division1 = ["@haxxorsid10 @haxxorsid11 @haxxorsid12", "@haxxorsid0"];
var i = 1;

window.onkeydown = function(e){

if(e.keyCode == 113){
document.getElementsByClassName("post-text-area")[0].value = division1[0];
document.getElementsByClassName("cmnt-btn")[0].click();


var repeat = setInterval(function start(){
if(i == division1.length - 1)
clearInterval(repeat);
document.getElementsByClassName("reply")[0].click();
document.getElementsByClassName("post-text-area")[1].value = division1[i];
document.getElementsByClassName("cmnt-btn")[1].click();
i++;
},2000);}};


Problem is even after all elements of division1 are commented, it continues commenting
undefined
in comments. It seems clearInterval is never executed.

Answer

Firs time click i=1 then 2nd time click it will count i=2.So the if condition not passing.so i was reset the i value at the time of clearinterval .So each time of you click count will restarted.

var division1 = ["@haxxorsid10 @haxxorsid11 @haxxorsid12", "@haxxorsid0"];
var i = 1;

window.onkeydown = function(e){

  if(e.keyCode == 113){ 
  

var repeat = setInterval(function start(){
  
 
if(i == division1.length - 1){
    clearInterval(repeat);
  i=0;//its will restart the count 2 time click
console.log('stop');
  }
  else{
  console.log('loop run')
  }
i++;
  },2000);}};

Comments