删除骨干视图下的Interval
Remove Interval in Backbone view
我正在做一个非常小的时钟练习与骨干,但我不能阻止它…我试图通过返回当前间隔的id来阻止它,但它不起作用。我做错了什么?
var app = {
lap: Backbone.Model.extend({
defaults: {
time: 0
}
}),
views: {
home: Backbone.View.extend({
el: $('#the-clock'),
tagName: 'div',
events: {
"click #save-lap" : "stopWatch"
},
render: function(){
var time = this.theWatch(),
tpl = '<button id="save-lap"></button><span id="clock">Son las <%= time %></span>';
$(this.el).html( _.template(tpl,time));
},
theWatch: function(){
var currentTime = new Date(),
currentHour = currentTime.getHours(),
currentMinutes = currentTime.getMinutes(),
currentSeconds = currentTime.getSeconds(),
timeOfDay = (currentHour < 12) ? "AM":"PM";
return data = {
time: currentHour + ':' + currentMinutes + ':' + currentSeconds + ' ' + timeOfDay
}
},
updateWatch: function(){
return setInterval(
function(){
this.render();
}.bind(this),
1000
)
},
stopWatch: function(){
console.log('stop')
var clock = this.updateWatch();
clearInterval(clock);
},
initialize: function(){
this.render();
this.updateWatch();
}
})
},
ini: function(){
var Home = new this.views.home();
}
};
(function(){
app.ini();
})();
谢谢!
通过调用this.updateWatch(),您将在每次想要停止时创建一个新的间隔!试试这个:
updateWatch: function(){
return setInterval(
function(){
this.render();
}.bind(this),
1000
)
},
stopWatch: function(){
clearInterval(this.clockInterval);
},
initialize: function(){
this.render();
this.clockInterval = this.updateWatch();
}
每次调用updateWatch时都在重新创建'Interval'对象。
相反,你需要将setInterval的结果存储在一个属性中;当你想停止它的时候可以重新使用
相关文章:
- 如何使用javascript从主svg对象动态创建svg视图框
- 正在使用$location.path(.)路由ng视图
- angular.js没有'无法在PhoneGap中处理视图标记
- 如何包含特定于每个视图angularjs的javascript文件
- 通过在Dojo mobile ViewController.openExternalView中动态更改打开同一外部视图的
- Kendo:我该如何在树视图中创建一个递归的hieiarchy
- Ajax Live搜索发布到Laravel视图
- backbone.js无法渲染视图
- 根据某些条件在视图之间切换
- ng视图外的链接重定向到ng视图内的页面
- 如何在Jquery中发布后将值从视图返回到控制器
- 将日期时间作为 JSON 发送将无法在我的视图中正确显示
- 使用Scala Play Framework视图中的键检索映射值
- FF视图源|脚本高亮显示为红色
- 如何使bxslider仅在移动视图中处于活动状态
- 为什么不't我的变量在我的控制器中填充后在我的视图中呈现
- 在不破坏未定义函数的情况下,对多个视图使用单个js文件
- 在Rails中更新Div,而不更改更新请求后的视图
- 如何在Ionic Android中将Javascript注入到web视图中
- 删除骨干视图下的Interval