I stated to work with vue.js recently and I don't understand why this behavior happens, using the setTimeout(). With the following code, the function defined in setInterval(function(), time) is launched immediately no matter the value of 'time':
timerOn(){
...
this.avatar.timer.data = setTimeout( this.timerFunction(), 10000);
},
timerFunction(){
...
console.log('Hello!!');
clearTimeout(this.avatar.timer.data);
this.timerOn();
},
But if I use the following code all works fine and the function inside setInterval occurs after the 'time' defined:
timerOn(){
...
var This = this;
this.avatar.timer.data = setTimeout(function() { This.timerFunction()}, 10000);
},
timerFunction(){
...
console.log('Hello!!');
clearTimeout(this.avatar.timer.data);
this.timerOn();
},
Someone can guide me and say why the first method fails?
Thank you.
this.timerFunctioninstead ofthis.timerFunction()?