dougupang0901 2018-04-13 14:47
浏览 786
已采纳

在Javascript百分比之间计算时间

I'm trying to work out a percentage from 0 - 100 from a part of HTML video

When the HTML video hits 5.89 secs I would like the animation to complete on 10.05 im having troubles doing this so far i have worked out the percentage of the time below

So there is 4.16 inbetween the times stated and it works out to be 1% is 0.041600000000000005

I have added the current time into the function becuase i need to work out what percent the video is on while its playing

var animationOneStart = 5.89;
var animationOneEnd = 10.05;


function perentageTime(animationStart,animationEnd,currentTime){
  var timeInbetween = animationEnd - animationStart;

  a = 1/100;
  b = a*timeInbetween;

  return b;
}

I'm having troubles working out the percentage while it playing would anyone be able to help me please?

Thanks

  • 写回答

1条回答 默认 最新

  • doutuo3575 2018-04-13 14:52
    关注

    The formula for percentage is (currentValue / maxValue) * 100 so, in your case will be (currentTime / timeInbetween) * 100

    Here is an example:

    var animationOneStart = 5.89;
    var animationOneEnd = 10.05;
    
    function perentageTime(animationStart,animationEnd,currentTime){
      var maxValue = animationEnd - animationStart;
      var ret = (currentTime / maxValue) * 100;
      return ret.toFixed(2);
    }
    
    var percentage = perentageTime(animationOneStart, animationOneEnd, 1);
    console.log(percentage);

    This will work only if currentTime starts from 0, if it starts from animationOneStart, you'll need to use ((currentTime - animationOneStart) / maxValue) * 100

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集