我在我的 React 应用程序中使用 videojs,我想实现这样的目标:
我拥有的东西是:
- 在变量中连续捕获 videojs 的当前时间。
- 一个 json 数组,每个数组都有 startTime 和 endTime 键。
我想要什么
- 我想检查当前时间是否介于 json 的 startTimes 和 endTimes 之间。
问题:
- 由于视频播放器的当前时间是一个不断变化的数量/值,我无法弄清楚如何检查它是否具有这么多 startTimeS 和 endTimeS 之间的值。
到目前为止我尝试过的(但显然这是错误的)
tags.map((item, i) => {
if( item.time <= currentTime <= item.stopTime){
this.props.markerReachedAction(i);
}
})
我该怎么做?
最佳答案
请使用由 &&
连接的两个隐式比较的条件,即
if (item.time <= currentTime && currentTime <= item.stopTime)
查看以下代码片段以了解问题所在。 1 < 3 < 2 应该为 false,但计算结果为 true。
$('#a').html('Result of 1 < 3 < 2 = ' + 1 < 3 < 2);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="a"></div>
由于该值不断更新,因此您也需要不断执行计算。您应该使用 setInterval与clearInterval一起。您可以使用引用链接上的用法示例。
关于javascript - 将 videojs 的 currentTime 与数字列表进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42247080/