我找到了这个区间函数,并且一直在我的网站上使用它。但是,我需要从间隔中获取总持续时间。我尝试对其进行修改,以便可以调用 timer.total_duration 来获取总时间,但是当我尝试运行修改后的代码时,我收到一条错误消息,提示“未捕获的 TypeError:数字不是函数”,修改后的函数是:
function interval(duration, fn) {
this.baseline = undefined
this.total_duration = 0.0
this.run = function () {
if (this.baseline === undefined) {
this.baseline = new Date().getTime()
}
fn()
var end = new Date().getTime()
this.baseline += duration
var nextTick = duration - (end - this.baseline)
if (nextTick < 0) {
nextTick = 0
}
this.total_duration += nextTick //line giving the error
(function (i) {
i.timer = setTimeout(function () {
i.run(end)
}, nextTick)
}(this))
}
this.stop = function () {
clearTimeout(this.timer)
}
}
知道为什么我会收到此错误,以及如何解决它以便获得总持续时间吗?