admin管理员组文章数量:1429067
Please, someone can explain me what is the meaning of (this) at the end of the function in a setInterval :
function Klass(name) {
this.name = name;
this.handle = null;
this.startTimer = function() {
this.handle = setInterval(function(obj) {
return(function() {
alert(obj.name);
});
}(this), 5000); // <-------------------- (this)
}
Please, someone can explain me what is the meaning of (this) at the end of the function in a setInterval :
function Klass(name) {
this.name = name;
this.handle = null;
this.startTimer = function() {
this.handle = setInterval(function(obj) {
return(function() {
alert(obj.name);
});
}(this), 5000); // <-------------------- (this)
}
Share
Improve this question
asked Mar 3, 2011 at 0:18
Daniel FloresDaniel Flores
7703 gold badges12 silver badges31 bronze badges
1 Answer
Reset to default 9The use of this
in the construct is intended to preserve the meaning of this
at the point setInterval
is called for the actual call back that is executed at the given interval. Without the manual preservation this
would bee the owner of the function at the point setInterval
was called.
Here's a very nice article on this subject
- http://www.quirksmode/js/this.html
Another way this could be done which may be a bit clearer is the following
var self = this
this.handle = setInterval(function() { alert(self.Name); }, 5000);
本文标签: javascriptsetInterval with (this)Stack Overflow
版权声明:本文标题:javascript - setInterval with (this) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745446656a2658694.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论