0

Hi I have a string a word currently and I'd like to send it in a socket like that :

If I have the word "problem", it send first : p, then pr, them pro, then prob, then probl, then proble, and finally problem but with a delay of x millisecond between each send so I've made this :

i is a random integer WORDS is an array of strings.

for (var l = 1; l <= WORDS[i].length; l++) {
   // send 1 ... WORD_LENGTH
   console.log(l);
   setTimeout(function () {
        // send length of the word and the word
        cconsole.log(l + ": " + WORDS[i].substring(0, l)); // send me a WORD directly on the first try
   }, 325 * l);
}

The problem is that it send me the word directly.

Thanks.

Evitareti
  • 41
  • 7
  • https://stackoverflow.com/questions/5226285/settimeout-in-for-loop-does-not-print-consecutive-values Found out. – Evitareti May 28 '17 at 21:27
  • Also see [closure inside loops](https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – James May 28 '17 at 21:28

0 Answers0