将 videojs 的当前时间与数字列表进行比较
Comparing currentTime of videojs with a list of numbers
我在我的 React 应用程序中使用了 videojs,我想实现这样的目标:
我有的东西是:
- videojs 的当前时间在变量中连续捕获。
- 一个 json 数组,每个数组都有 startTime 和 endTime 键。
我想要的
- 我想检查当前时间的值是否介于 json 的任何 startTimes 和 endTimes 之间。
问题:
- 由于视频播放器的当前时间是不断变化的quantity/value,我无法弄清楚如何检查它是否具有这么多 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 应该为假,但计算结果为真。
$('#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 一起使用。参考 link 上有一些用法示例,您可以使用。
我在我的 React 应用程序中使用了 videojs,我想实现这样的目标:
我有的东西是:
- videojs 的当前时间在变量中连续捕获。
- 一个 json 数组,每个数组都有 startTime 和 endTime 键。
我想要的
- 我想检查当前时间的值是否介于 json 的任何 startTimes 和 endTimes 之间。
问题:
- 由于视频播放器的当前时间是不断变化的quantity/value,我无法弄清楚如何检查它是否具有这么多 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 应该为假,但计算结果为真。
$('#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 一起使用。参考 link 上有一些用法示例,您可以使用。