admin管理员组文章数量:1291800
I'm trying to access this.collection
from within a setInterval
. However, this isn't bound. I can't seem to figure out how to bind it so that this
can access the collection, not the setInterval
object.
Here's a snipit from my View.
initialize: function(){
_.bindAll(this);
},
start: function() {
setInterval(function() {
this.collection.each(function(item) {
console.log(item.id);
});
}, 5000);
}
Any suggestions?
I'm trying to access this.collection
from within a setInterval
. However, this isn't bound. I can't seem to figure out how to bind it so that this
can access the collection, not the setInterval
object.
Here's a snipit from my View.
initialize: function(){
_.bindAll(this);
},
start: function() {
setInterval(function() {
this.collection.each(function(item) {
console.log(item.id);
});
}, 5000);
}
Any suggestions?
Share Improve this question edited Jun 27, 2012 at 21:21 mu is too short 435k71 gold badges858 silver badges818 bronze badges asked Jun 27, 2012 at 20:34 dzmdzm 23.6k51 gold badges152 silver badges229 bronze badges2 Answers
Reset to default 8You should bind()
the value of this
you need when you set up the callback:
setInterval(function() {
this.collection.each(function(item) {
console.log(item.id);
});
}.bind(this), 5000);
Don't forget to include the shim from the above MDN page if you need IE8 patibility.
You can do this:
initialize: function(){
_.bindAll(this);
},
afterInterval: function() {
this.collection.each(function(item){
console.log(item.id);
});
},
start: function() {
setInterval(this.afterInterval, 5000);
}
afterInterval
is now a callback and it has access to this
of the view.
本文标签: javascriptBackbonejs bind this to setIntervalStack Overflow
版权声明:本文标题:javascript - Backbone.js bind this to setInterval - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741540653a2384300.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论