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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题