DOM not modified by var


#1

why my DOM is not modified by this.tempo ?

{{ this.mensagemestimada }} {{ this.tempo }}
``` public temporizador(){ var counter = 0; var timer = setInterval(function() { if( counter >= 10 ) { clearInterval( timer ); }
this.tempo = counter;
console.log(this.tempo);
console.log( counter);
counter++;
}, 1000);

}


#2

Remove this, use like {{ tempo }}. Just use ‘this.tempo’ to your .js file.


#3

DOM not modified…other solution ?


#4

In any case, don’t use oldschool functions. You will loose scope of this.

public temporizador(){
    let counter = 0;
    let timer = setInterval( () => {
        if( counter >= 10 ) {
          clearInterval( timer );
        }

        this.tempo = counter;
        console.log(this.tempo);
        console.log( counter);
        counter++;
    }, 1000);
  }

#5

Only because “function” ? WTF!


#6

No, not wtf. Just basic es6 scoping with this :wink:


#7

Thanks my friend! You are welcome!