Szilvia Németh Szilvia Németh - 4 months ago 20
Javascript Question

Delay in javascript

I saw a lot of questions about how to put delay or sleep in javascript. I tried everything i just could not have the code work, like i wanted to.

So i get like 500 friend requests on facebook, kinda annoying accepting one them one by one. So i found the script which does accept all my friend requests at once. I tried the set time out and sleep codes, which actually do the work but i want to put the delay between each actions. With Set timeout, it just delays the whole script. So i wanna do like this:

Confirm

1sec delay

Confirm

1sec delay

confirm

1sec deleay...

Here is my original script:

var field = document.getElementsByName("actions[accept]");
for (i = 0; i < field.length; i++)
field[i].click() ;

Answer

The correct solution to your problem would be the following:

var field = document.getElementsByName("actions[accept]");
var index = 0;
function acceptFriends(){
    if(index<field.length){
      field[index].click();
      index++;
      setTimeout(acceptFriends,1000);
    }
}
acceptFriends();
Comments