Vue.js计时计算与普通JavaScript版本不匹配

Vue.js计时计算与普通JavaScript版本不匹配,javascript,vue.js,vuejs2,Javascript,Vue.js,Vuejs2,我正在尝试创建一个“每分钟节拍”(BPM)计算器,与您可以找到的完全相同(目前)。但出于某种原因,当我在测试歌曲的该链接上使用BPM计算器时,在7次按键中,它与实际值85.94的偏差在1 BPM以内,从那时起变得更准确,在实际BPM的0.05以内结束,而在我的(基本上编码相同的)Vue.js版本中,它的起点要高得多(182-->126-->110)从那里往下走,但即使按了60次键,它仍然会以约2 BPM的速度关闭,而在一首完整的歌曲之后,它仍然会以约0.37 BPM的速度关闭 以下是代码: 以下

我正在尝试创建一个“每分钟节拍”(BPM)计算器,与您可以找到的完全相同(目前)。但出于某种原因,当我在测试歌曲的该链接上使用BPM计算器时,在7次按键中,它与实际值85.94的偏差在1 BPM以内,从那时起变得更准确,在实际BPM的0.05以内结束,而在我的(基本上编码相同的)Vue.js版本中,它的起点要高得多(182-->126-->110)从那里往下走,但即使按了60次键,它仍然会以约2 BPM的速度关闭,而在一首完整的歌曲之后,它仍然会以约0.37 BPM的速度关闭

以下是代码:

以下是我的版本:

computed: {
    tappedOutBpm: function() {
        let totalElapsedSeconds = (this.timeOfLastBpmKeypress - this.timeOfFirstBpmKeypress) / 1000.0
        let bpm = (this.numberOfTapsForBpm / totalElapsedSeconds) * 60.0
        return Math.round(100*bpm)/100;
    },
},
methods: {
    tapForBPM: function() {
        let now = new Date;
        now = now.getTime();
        // let now = window.performance.now()
        if (this.timeOfFirstBpmKeypress === 0 || now - this.timeOfLastBpmKeypress > 5000) {
            this.timeOfFirstBpmKeypress = now
            this.timeOfLastBpmKeypress = now
            this.numberOfTapsForBpm = 1
        } else {
            this.timeOfLastBpmKeypress = now
            this.numberOfTapsForBpm++
        }
    }
}

我通过单步执行我们的两个代码找到了答案


问题是,当用户第一次点击按键时,我就将点击次数设置为
1
,而实际上,我想计数的不是点击次数,而是节拍,第一个节拍不需要一次点击,而是两次:节拍的开始和结束。因此,我应该做的是将变量重命名为
numberOfTappedOutBeats
,并在第一次点击后将其设置为
0
,而不是
1

,我通过单步执行两个代码来计算出来

问题是,当用户第一次点击按键时,我就将点击次数设置为
1
,而实际上,我想计数的不是点击次数,而是节拍,第一个节拍不需要一次点击,而是两次:节拍的开始和结束。因此,我应该将变量重命名为
numberOfTappedOutBeats
,并在第一次点击后将其设置为
0
,而不是
1

computed: {
    tappedOutBpm: function() {
        let totalElapsedSeconds = (this.timeOfLastBpmKeypress - this.timeOfFirstBpmKeypress) / 1000.0
        let bpm = (this.numberOfTapsForBpm / totalElapsedSeconds) * 60.0
        return Math.round(100*bpm)/100;
    },
},
methods: {
    tapForBPM: function() {
        let now = new Date;
        now = now.getTime();
        // let now = window.performance.now()
        if (this.timeOfFirstBpmKeypress === 0 || now - this.timeOfLastBpmKeypress > 5000) {
            this.timeOfFirstBpmKeypress = now
            this.timeOfLastBpmKeypress = now
            this.numberOfTapsForBpm = 1
        } else {
            this.timeOfLastBpmKeypress = now
            this.numberOfTapsForBpm++
        }
    }
}