0
var delayTime = 2000;
for(var i = 0; i<this.textToWrite.length; i++){
  setTimeout(
    (
      function (s){
        return function () {
          this.writeText += s;
          console.log(this.writeText);
        }
      }(this.textToWrite[i])
    ), delayTime)
  delayTime += 2000;
}

have some problem with this.writeText. It's global variable but when i even comment += s line then i have undefined value... (I set writeText: string = "" globaly) Is this.writeText reference to global variable? How can I access global variables in this example? I need assing char from textToWrite object to writeText with 2s delay.

Catalyst
  • 1,018
  • 2
  • 12
  • 19
Dominik
  • 37
  • 1
  • 1
  • 9

2 Answers2

0

The problem can be that you used function word.

setTimeout(
((s) => {
    return () => {
      this.writeText += s;
      console.log(this.writeText);
    }
  }(this.textToWrite[i])
), delayTime)

Another way is to use it without this if its global variable:

 setTimeout(
((s) => {
    return () => {
      writeText += s;
      console.log(this.writeText);
    }
  }(this.textToWrite[i])
), delayTime)
halfer
  • 19,824
  • 17
  • 99
  • 186
alexKhymenko
  • 5,450
  • 23
  • 40
0

Only use arrow functions

settimeout(x => {
   console.log(x);
}, 1000);

not like this and it will not accept out side parameters

settimeout(funtion(x) {
   console.log(x);
}, 1000);
eko
  • 39,722
  • 10
  • 72
  • 98
Sheik Althaf
  • 1,595
  • 10
  • 16