我最近声明要使用 vue.js,但我不明白为什么会使用 setTimeout() 发生这种行为。使用以下代码,无论 'time' 的值如何,setInterval(function(), time) 中定义的函数都会立即启动:
timerOn(){
  ...
  this.avatar.timer.data = setTimeout( this.timerFunction(), 10000);
},
timerFunction(){
  ...
  console.log('Hello!!');
  clearTimeout(this.avatar.timer.data);
  this.timerOn();
},
Run Code Online (Sandbox Code Playgroud)
但如果我使用以下代码,一切正常,并且 setInterval 内的函数在定义的“时间”之后发生:
timerOn(){
  ...
  var This = this;
  this.avatar.timer.data = setTimeout(function() { This.timerFunction()}, 10000);
},
timerFunction(){
  ...
  console.log('Hello!!');
  clearTimeout(this.avatar.timer.data);
  this.timerOn();
},
Run Code Online (Sandbox Code Playgroud)
有人可以指导我并说为什么第一种方法失败了?
谢谢。