0

I want to make a GreaseMonkey script. I want to have a setTimeout at the end of my while, but I don't know how to do that.

run();
function run(){
  var klovas = document.getElementById("light").innerHTML;
  var btn = document.getElementsByClassName("farm_icon farm_icon_a");
  if(klovas < 6){
    alert("Kevés egység van");
  } else {
    var i = 0;
    while (i < btn.length){
      if(typeof btn[i] != "undefined"){
        btn[i].click();
      }
    i++;
    setTimeout("run()", 3000);
    }
  }
}

With this code, the problem is that the setTimeout is not working and doesn't wait 3 seconds like it is supposed to.

I tried other ways, but nothing has worked.

EDIT

function run(){
  var klovas = document.getElementById("light").innerHTML;
  var btn = document.getElementsByClassName("farm_icon farm_icon_a");
  if(klovas < 2){
    alert("Kevés egység van");
  } else {
    var i = 0;
    while (i < btn.length){
      if(typeof btn[i] != "undefined"){
        btn[i].click();
      }
    i++;    
    }
  }
}
setInterval(run, 6000); 

I tryed this. Its runing every 6 sec, but i get error in website, that i cand click more than 5 times in a sec. So waiting 6secound when i open the page, and after click, and i get error. Its not jet working. :(

Dani Misi
  • 25
  • 5

2 Answers2

0

There are a few problems here.

Firstly, while setTimeout can accept a function as its first argument, you would want to pass the function as an argument, not execute the function, so you should replace "run()" with run.

Secondly, I'm not exactly sure why you're recursing -- why is setTimeout inside run()? Instead of putting it inside your run function, try putting it at the bottom, and deleting the run() call at the top. As far as I can tell, there's no reason that this code needs to recurse at alll.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
furkle
  • 5,019
  • 1
  • 15
  • 24
  • @Xufox My mistake! I'll edit that right now. – furkle Jan 18 '17 at 20:26
  • I need a break after i++. Now i tryed to alert the btn[i], and when i get up the alert, and i press ok, then working good. just i must always press ok. :D function run(){ var klovas = document.getElementById("light").innerHTML; var btn = document.getElementsByClassName("farm_icon farm_icon_a"); if(klovas < 2){ alert("Not enought light"); } else { var i = 1; while(i < btn.length){ btn[i].click(); i++; alert(btn[i]); } } } setInterval(run, 5000); Like this – Dani Misi Jan 19 '17 at 01:22
0

If you wanted it to only trigger once:

function run(){
    var data = [1,2,3];
    var i = 0;
    while (i < data.length) {
        console.log(data[i]);
        i++;
    }
}

setTimeout(run, 3000);

The way you wrote it now, it would repeat every 3 seconds.

function run(){
    var data = [1,2,3];
    var i = 0;
    while (i < data.length) {
        console.log(data[i]);
        i++;
    }
    setTimeout(run, 3000);
}

run();

But setInterval would accomplish the same results.

function run(){
    var data = [1,2,3];
    var i = 0;
    while (i < data.length) {
        console.log(data[i]);
        i++;
    }
}

setInterval(run, 3000);

EDIT

User wanted to see what would happen if you call setInterval from inside the callback function. Note that the number of intervals grows exponentially every 3 seconds.

setInterval causes the function to run every 3 seconds, while setTimeout causes the function to run once in 3 seconds.

var numberOfIntervals = 0;

function run(){
    setInterval(run, 3000);
    numberOfIntervals++;
    console.log(numberOfIntervals);
}

run();
GantTheWanderer
  • 1,255
  • 1
  • 11
  • 19