更新时间:2023-02-26 08:05:05
如果 setInterval
调用函数它没有像你期望的那样设置这个
值。调用 this.tick()
正确设置它,但只是传递函数并以另一种方式调用它不会。您必须将此
值绑定到您想要的值:
If setInterval
calls the function it doesn't set the this
value as you may expect. Calling this.tick()
does set it correctly, but just passing the function and having it called in another way does not. You have to bind the this
value to what you want:
setInterval(this.tick.bind(this), 1000);
这适用于较新的浏览器,但有可用垫片。
This is available on newer browsers but there are shims available.
另外,你可能意味着 this.sessionExpired()
,就像它定义的那样。 return
没有多大意义,因为 setInterval
并不关心返回值。
Also, you probably meant this.sessionExpired()
, as that's where it's defined. return
doesn't make much sense there since setInterval
does not care about the return value.