Nodejs:clearInterval() 使用 'this'
Nodejs : clearInterval() using 'this'
我想要 clearInterval() 并且我可以使用 clearInterval(myInterval) 来实现,但为什么我不能使用 clearInterval(this)?
这是有效的代码:
var test = setInterval(function(){
request.post({url: myURL, form: {
user : myUser,
pass : myPass
function(err,res,body){
if(res.statusCode === 302) clearInterval(test);
})
}, 1100)
这是不起作用的代码:
setInterval(function(){
var that = this;
request.post({url: myURL, form: {
user : myUser,
pass : myPass
function(err,res,body){
if(res.statusCode === 302) clearInterval(that);
})
}, 1100)
编辑 1:对于这个糟糕的问题,我深表歉意。我不太熟悉 'this' 的概念,并且直觉地认为使用 'this' 我可以 clearInterval()。这样做的原因是因为当我在第一个代码中 console.log(test) 和在 setInterval 函数中的第二个代码中 console.log(this) 时,输出是相同的,因此直觉。好吧,我宁愿研究'this'。谢谢大家的回答和评论。非常感谢。
setInterval()
没有在 this
的值中提供 timerID。你不能那样使用它。 timerID 仅作为第一个示例中 setInterval()
的 return 值提供。
您可以创建自己的小定时器对象,封装您想要的东西,为您存储 timerID。
例如,您可以像这样创建自己的计时器对象,将计时器对象作为 this
的值传递给回调。然后你可以使用 this
在对象上调用 clearInterval()
方法。
class IntervalTimer() {
start(cb, t) {
// if there was a previous interval going here, stop it
// only one per timer object permitted
this.stop();
this.id = setInterval(() => {
cb.call(this); // set the this value to be our object
}, t);
}
stop() {
if (this.id) {
clearInterval(this.id);
this.id = null;
}
}
}
// usage
let t = new IntervalTimer();
t.start(function() {
// based on some logic, decide to clear the interval
// the value of "this" has been set to the timer object
this.stop();
}, 1000);
有问题,在setInterval回调函数中"this"表示context。在第一个函数中,您编写的上下文与计时器回调函数不同。上下文很重要,因此使用 clearInterval 必须将参数作为整数而不是上下文本身传递。
我想要 clearInterval() 并且我可以使用 clearInterval(myInterval) 来实现,但为什么我不能使用 clearInterval(this)?
这是有效的代码:
var test = setInterval(function(){
request.post({url: myURL, form: {
user : myUser,
pass : myPass
function(err,res,body){
if(res.statusCode === 302) clearInterval(test);
})
}, 1100)
这是不起作用的代码:
setInterval(function(){
var that = this;
request.post({url: myURL, form: {
user : myUser,
pass : myPass
function(err,res,body){
if(res.statusCode === 302) clearInterval(that);
})
}, 1100)
编辑 1:对于这个糟糕的问题,我深表歉意。我不太熟悉 'this' 的概念,并且直觉地认为使用 'this' 我可以 clearInterval()。这样做的原因是因为当我在第一个代码中 console.log(test) 和在 setInterval 函数中的第二个代码中 console.log(this) 时,输出是相同的,因此直觉。好吧,我宁愿研究'this'。谢谢大家的回答和评论。非常感谢。
setInterval()
没有在 this
的值中提供 timerID。你不能那样使用它。 timerID 仅作为第一个示例中 setInterval()
的 return 值提供。
您可以创建自己的小定时器对象,封装您想要的东西,为您存储 timerID。
例如,您可以像这样创建自己的计时器对象,将计时器对象作为 this
的值传递给回调。然后你可以使用 this
在对象上调用 clearInterval()
方法。
class IntervalTimer() {
start(cb, t) {
// if there was a previous interval going here, stop it
// only one per timer object permitted
this.stop();
this.id = setInterval(() => {
cb.call(this); // set the this value to be our object
}, t);
}
stop() {
if (this.id) {
clearInterval(this.id);
this.id = null;
}
}
}
// usage
let t = new IntervalTimer();
t.start(function() {
// based on some logic, decide to clear the interval
// the value of "this" has been set to the timer object
this.stop();
}, 1000);
有问题,在setInterval回调函数中"this"表示context。在第一个函数中,您编写的上下文与计时器回调函数不同。上下文很重要,因此使用 clearInterval 必须将参数作为整数而不是上下文本身传递。