我在 javascript 中有一个 simpe 对象,它的方法很少。其中两个我想用window.setTimeout
函数定期调用。我当前的代码如下所示。
var shakeResolver = function () {
var resolveTimeout;
console.log(this);
var context = this;
this.startShakeResolve = function () {
this.resolveTimeout = window.setTimeout(this.call(context.stopShakeResolve, context), 2000);
$(window)
.on('devicemotion', this.onDeviceMotion);
};
this.onDeviceMotion = function (event) {};
this.stopShakeResolve = function (context) {
this.resolveTimeout = window.setTimeout(context.startShakeResolve, settings.interval);
};
}
问题显然是我误解了范围是如何工作的,看起来从超时调用函数时,它是从另一个实际上不存在的上下文中调用的?