如果我说得对,那么我会做这样的事情:
var Callout = Backbone.View.extend({
events: {
"click": "clearTimeout" // clear timeout if clicked inside
},
render: function() {
// render and then
this.setTimeout();
return this;
},
setTimeout: function() {
var self = this;
if (!this.timeout) {
this.timeout = setTimeout(function() {self.hide();}, 120 * 1000);
}
return this;
},
clearTimeout: function(){
if (this.timeout) {
clearTimeout(this.timeout);
}
return this;
},
hide: function() {
this.$el.hide();
return this;
}
)}
如果您需要重新设置超时,那么事件哈希将如下所示(而不是模糊事件, jQuery 中的focusout事件气泡)
events: {
"focusout": "setTimeout",
"focusin": "clearTimeout"
}
UPD:为确保focusout
将焦点移出Callout
视图,您需要检查事件的relatedTarget是否位于视图内部$el
。