Javascript 有人知道在这个时间间隔中有什么东西是不清楚的吗?
这是我从咖啡中转换出来的js:Javascript 有人知道在这个时间间隔中有什么东西是不清楚的吗?,javascript,loops,intervals,Javascript,Loops,Intervals,这是我从咖啡中转换出来的js: set_timer: function() { var _this = this; return this.timer = setInterval(function() { _this.set({ time_to_complete: _this.get("time_to_complete") + 1 }); if (_this.get("time_to_complete") > 3) { console
set_timer: function() {
var _this = this;
return this.timer = setInterval(function() {
_this.set({
time_to_complete: _this.get("time_to_complete") + 1
});
if (_this.get("time_to_complete") > 3) {
console.log("End of clear.");
return _this.reset_timer(_this.timer);
}
}, 1000);
},
reset_timer: function() {
clearInterval(this.timer);
return this.set({
time_to_complete: 0
});
}
然后它被称为:
this.model.set_timer();
由于某些原因,这并不清楚,我的时间间隔一直在生成那些console.log
下面是另一个相同错误的示例,但是在Coffeescript中,将空格命名为$
,作为下划线的mixin方法
set_timer: (model) =>
$.timer = setInterval =>
model.set time_to_complete: model.get("time_to_complete") + 1
if model.get("time_to_complete") > 3
console.log "End of clear."
_.reset_timer model
, 1000
reset_timer: (model) ->
clearInterval $.timer
model.set time_to_complete: 0
在清除函数之前,已返回该函数。先清除它,然后返回
reset_timer: function() {
clearInterval(this.timer);
return this.set({
time_to_complete: 0
});
}
至于
this
的值,要小心,因为this
是由您调用它的方式决定的,而不是由它的声明方式决定的。在清除它之前,您已经返回了该函数。先清除它,然后返回
reset_timer: function() {
clearInterval(this.timer);
return this.set({
time_to_complete: 0
});
}
至于
this
的值,要小心,因为this
是由您调用它的方式决定的,而不是由它的声明方式决定的。好的,所以如果我将它添加到set\u timer函数的开头,它就工作了
if (this.timer !== null) {
clearInterval(this.timer);
}
好的,如果我把它添加到我的set_定时器函数的开头,它就可以工作了
if (this.timer !== null) {
clearInterval(this.timer);
}
你需要展示一下它的名字。我的猜测是,
此
未按预期绑定。我猜此
(在您的clearInterval中)不是您所认为的。您需要显示它的调用方式。我猜这个
没有像预期的那样绑定。我猜这个
(在你的clearInterval中)不是你想象的那样。哦。我不敢相信我没有看到我更新了我的答案,尽管它仍然不起作用。同样的issue@Trip如何在setInterval
function@Trip然后是计时器。计时器返回一个引用计时器id的整数。想不出其他任何东西。噢。我不敢相信我没有看到我更新了我的答案,尽管它仍然不起作用。同样的issue@Trip如何在setInterval
function@Trip然后是计时器。计时器返回一个引用计时器id的整数。想不出其他任何东西。