-2

I need to add a delay inside a loop into this javascript code but can't figure out how :

var hossam = document.getElementsByClassName('_42ft _4jy0 _4jy3 _517h _51sy');

for (var i = 0; i < hossam.length; i++) {
    hossam[i].click();
}

alert('[hossam] ' + hossam.length + ' people are now unfollowed! >:) ');
Gio
  • 3,242
  • 1
  • 25
  • 53
user205537
  • 13
  • 4
  • 8
    It looks like there's a loop – Denys Séguret Jan 06 '15 at 09:17
  • why you want to add delay in for loop any reason? – Girish Jan 06 '15 at 09:20
  • Pls, clarify what you exactly want. Add delay between each `for` iteration? Why? – hindmost Jan 06 '15 at 09:20
  • possible duplicate of [How do I add a delay in a JavaScript loop?](http://stackoverflow.com/questions/3583724/how-do-i-add-a-delay-in-a-javascript-loop) – Rhumborl Jan 06 '15 at 09:21
  • 1
    As for delays, you don't just stick a delay in -- JS doesn't like running synchronously. If you did, most browsers would appear to freeze til the loop finished, and then you'd see all the changes at once anyway. – cHao Jan 06 '15 at 09:21
  • @Girish: this is a question-and-answer site; I don't think it's your place to ask _why_ he wants this functionality, but simply to help solve his problem. I always feel a degree of frustration when people ask _why_ I want something rather than helping me. For all you know, the script in his question is an incredibly simplified abstraction of the real problem he's working on. – awj Jan 06 '15 at 09:23
  • 4
    @awj: This is a question-and-answer site aimed at providing non-crappy answers. It's not our place to sit by and watch someone go about solving the wrong problem. I think it's *entirely* appropriate to ask why they're going down that road, and to steer them in a different direction if what they're literally asking for isn't the best answer. – cHao Jan 06 '15 at 09:29
  • @awj we all are for help, `op` has not mentioned problem, he only saying **add delay in loop** so i have asked to add real issue, how can you provide solution without problem?? – Girish Jan 06 '15 at 09:30
  • I need to add a delay into "for" loop so i get one click per second. I know about browser freeze and this not a problem for me. – user205537 Jan 06 '15 at 09:42
  • You don't need to know the problem to answer my question i asked a clear question. – user205537 Jan 06 '15 at 09:44
  • @Gio Read my question again i didn't ask to "add a loop"! i aksed how to add delay into a loop!! – user205537 Jan 06 '15 at 09:47
  • @user205537 see link http://stackoverflow.com/questions/3583724/how-do-i-add-a-delay-in-a-javascript-loop – Girish Jan 06 '15 at 09:49
  • 2
    @user205537: Read your own question again. It says, word-for-word, "I need to add a loop into this javascript code but can't figure out how". – cHao Jan 06 '15 at 11:30

1 Answers1

1

You can make a recursive function call with a delay.

i = 0;
DELAY_IN_MILLISEC = 1000;

function foo() {
    if (i++ < hossam.length) {
        hossam[i].click();
        setTimeout(function(){foo()}, DELAY_IN_MILLISEC);
    }
}

foo();
Gio
  • 3,242
  • 1
  • 25
  • 53
  • `setTimeout` takes milliseconds as the second param, not seconds. Consequently your answer incurs a delay of 1 millisecond between each pass. – awj Jan 06 '15 at 09:57
  • Haven't you missed some invocation of `foo()`? And where does `i` get incremented? If this answer is intended to be placed inside the OP's `for` loop then why is `i` declared, and why the need for `foo()`? – awj Jan 06 '15 at 10:07
  • [a] foo() is called by setTimeout (corrected) [b] i gets incremented here: `if (i++ < hossam.length) {` [c] My recommendation is to use a recursive method to achieve the desired behavior because it is not possible to achieve the desired behavior through a for loop. My comment regarding your proposed anwser explains why. – Gio Jan 06 '15 at 10:12
  • Apologies - I didn't see the inner (recursive) `foo()` call. However, aren't you still missing an invocation kick things off? – awj Jan 06 '15 at 10:17
  • Yes `foo()` needs to be called in order to kick things off (added). – Gio Jan 06 '15 at 10:19
  • Why not just change `foo` to an IIFE? _I.e._, `var foo = function () { ...}();` Then you can place all three declarations in a single `var` statement (which you've also omitted). – awj Jan 06 '15 at 10:21
  • [a] I'm sure things can be refactored to less code, but this doesn't necessarely provided a better explanation. [b] var is not necessary in this case, `i` and `DELAY_IN_MILLISEC` are both in global scope. – Gio Jan 06 '15 at 10:29