-1

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() ;
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • 1
    the question is: why would you get that many friend requests, and why would you accept all of them without even checking if you know them? – andyrandy Jul 15 '16 at 10:40
  • Basic facebook promotion :) – Szilvia Németh Jul 15 '16 at 11:41
  • 1
    you do know that you are not allowed to use user profiles for promotional stuff, right? just saying. user accounts have to be used with a real name of a real person and they can´t be used for commercial reasons. also, friends are limited to 5000 for user profiles. – andyrandy Jul 15 '16 at 12:08

3 Answers3

1
var field = document.getElementsByName("actions[accept]");

for (i = 0; i < field.length; i++){
  setTimeout(function(){field[i].click()},(i*1000))
}
Matz Reckeweg
  • 31
  • 1
  • 4
1

Here is another solution for this, we need to call immediate function with "i" param to get it correct into setTimeout:

var field = document.getElementsByName("actions[accept]");

for (var i = 0; i < field.length; i++) {
  (function(i){ 
    setTimeout(function() {
      field[i].click();
    }, i * 1000);
  })(i);
}

But I suggest to use recursion answer above as better solution for this, because good practise not to use functions in loop. Here is reference : https://jslinterrors.com/dont-make-functions-within-a-loop

  • @MarkoMackic you forget to put output here, and please learn a bit javascript :) –  Jul 15 '16 at 09:29
  • my mistake :) you're correct it's working, but what it says is "Don't make functions within loop" quoted.. :) – Marko Mackic Jul 15 '16 at 09:30
  • @MarkoMackic waiting you to remove "minus" :P who says ? This immediate function good pattern for this issue. –  Jul 15 '16 at 09:31
  • Edit something simple in answer because I mislead myself in downvoting your answer without proper test :) I apologize – Marko Mackic Jul 15 '16 at 09:35
  • @luschn I think it's more readable then recursion above, why do you think that it's bad to call anonymous function each loop ? –  Jul 15 '16 at 10:59
  • i think markos solution is the only correct one. your solution may be more readable for you, but you are creating new functions all the time. same goes for the solution of matz. read about javascript performance optimization. – andyrandy Jul 15 '16 at 11:01
  • @luschn matz solution not works at all, I wrote why under that answer, can you explain why it's not correct create new functions ? –  Jul 15 '16 at 11:02
  • well, whatever. it´s as bad as your solution, i´m afraid. for example: http://stackoverflow.com/questions/10320343/dont-make-functions-within-a-loop – andyrandy Jul 15 '16 at 11:03
  • you should always use eslint (or jshint/jslint) to check your code, it´s a well known rule not to create functions in a loop. – andyrandy Jul 15 '16 at 11:04
  • @luschn thanks, I've edited my answer and add reference about not to use function in loop, but still my solution works correct I leave it too, for someone who want to learn JS loops and how it works. –  Jul 15 '16 at 11:48
0

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();
Marko Mackic
  • 2,293
  • 1
  • 10
  • 19