简体   繁体   English

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

[英]Vue.js timing calculations are not matching plain JavaScript version

I'm trying to create a 'beats per minute' (BPM) calculator, identical (for now) to the one you can find here . 我正在尝试创建一个“每分钟心跳数”(BPM)计算器,该计算器(现在)与您可以在此处找到的相同。 But for some reason, when I use the BPM calculator at that link on a test song, it gets within 1 BPM of the actual value of 85.94 within of 7 keypresses and just gets more accurate from there, ending within 0.05 of the actual BPM, whereas with my (essentially identically-coded) Vue.js version, it starts much higher (182-->126-->110) and goes down from there, but even after 60 keypresses it's still off by ~2 BPM, and after a full song, it was still off by about 0.37 BPM. 但是由于某种原因,当我在测试歌曲的那个链接上使用BPM计算器时,它在7次按键操作中的实际值在85.94的实际值的1 BPM之内,并且从那里得到的精度更高,在实际BPM的0.05之内,而在我的Vue.js版本(基本上相同的编码)中,它的起始位置要高得多(182-> 126-> 110),然后从那里下降,但是即使经过60次按键操作,它仍然关闭了约2 BPM,然后一首完整的歌曲,它仍然下降了约0.37 BPM。

Here's the code for the plain-JavaScript version at that link : 这是该链接上的纯JavaScript版本的代码:

var count = 0;
var msecsFirst = 0;
var msecsPrevious = 0;

function ResetCount()
  {
  count = 0;
  document.TAP_DISPLAY.T_AVG.value = "";
  document.TAP_DISPLAY.T_TAP.value = "";
  document.TAP_DISPLAY.T_RESET.blur();
  }

function TapForBPM(e)
  {
  document.TAP_DISPLAY.T_WAIT.blur();
  timeSeconds = new Date;
  msecs = timeSeconds.getTime();
  if ((msecs - msecsPrevious) > 1000 * document.TAP_DISPLAY.T_WAIT.value)
    {
    count = 0;
    }

  if (count == 0)
    {
    document.TAP_DISPLAY.T_AVG.value = "First Beat";
    document.TAP_DISPLAY.T_TAP.value = "First Beat";
    msecsFirst = msecs;
    count = 1;
    }
  else
    {
    bpmAvg = 60000 * count / (msecs - msecsFirst);
    document.TAP_DISPLAY.T_AVG.value = Math.round(bpmAvg * 100) / 100;
    document.TAP_DISPLAY.T_WHOLE.value = Math.round(bpmAvg);
    count++;
    document.TAP_DISPLAY.T_TAP.value = count;
    }
  msecsPrevious = msecs;
  return true;
  }
document.onkeypress = TapForBPM;

//  End -->

And here's my version: 这是我的版本:

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++
        }
    }
}

I figured it out by stepping through both of our code. 我通过逐步完成这两个代码来弄清楚。

The problem was that I was setting the number of taps to 1 as soon as the user tapped the key the first time, when in reality it's not taps that I want to count, but beats , and the first beat requires not one tap, but two : the start and the end of that beat. 问题是,我设置水龙头的数量1 ,一旦用户点击该按键上的第一次,而实际上这并不是说我想算水龙头 ,而是节奏 ,和第一拍要求不是一个水龙头,但 :那拍子的开始和结束。 So what I should do is rename the variable to numberOfTappedOutBeats and set it to 0 after the first tap rather than 1 . 所以我应该做的是将变量重命名为numberOfTappedOutBeats并在第一次点击后将其设置为0而不是1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM