[英]How can I loop through this array using javascript?
鉴于此脚本:
<html>
<head>
<script>
var sentences=
[
['journey27.mp4',
[
[1, 2],
[5, 7]
]
],
['journey28.mp4',
[
[10, 12],
[13, 17],
[25, 36]
]
],
['journey30.mp4',
[
[13, 15],
[15, 19],
[21, 30]
]
]
];
function playVideo(myUrl, startTime, endTime) {
var video = document.getElementById("myVideo");
video.src = myUrl;
video.currentTime=startTime;
video.play();
var maxTime = endTime;
video.addEventListener("timeupdate", function(){
if(video.currentTime >= maxTime){
playVideo(myUrl, startTime, endTime);
}
}, false);
}
function myLoop(){
I want to loop each pairs 5 times before jump to the next pair
}
</script>
</head>
<body>
<video id="myVideo" width="1000" height="800" controls autoplay loop>
Your browser does not support the video tag.
</video>
<br>
<a onclick="myLoop()" > Play </a>
</body>
</html>
现在我要填写myLoop
function。
我想在跳转到数组中的下一对之前循环每对 5 次。
例如,-第一对:'journey27.mp4',循环“ [1, 2]
”5次
然后是下一对:'journey27.mp4',循环“ [5, 7]
”5 次
然后是下一对:'journey28.mp4',循环“ [10, 12]
”5 次
然后是下一对:'journey28.mp4',循环“ [13, 17]
”5 次
等等
注意:上面的playVideo
function 有效
如何使用 javascript 遍历这个数组?
使用全局变量来保存视频的当前索引和重复次数。 增加重复计数,当达到5
时增加索引。
没有循环,迭代发生在用户单击链接时。
var sentences= [ ['journey27.mp4', [ [1, 2], [5, 7] ] ], ['journey28.mp4', [ [10, 12], [13, 17], [25, 36] ] ], ['journey30.mp4', [ [13, 15], [15, 19], [21, 30] ] ] ]; function playVideo(myUrl, startTime, endTime) { console.log(`Playing ${myUrl} from ${startTime} to ${endTime}`); } let video_index = 0; let time_index = 0; let repeat = 0; function myLoop(){ playVideo(sentences[video_index][0], sentences[video_index][1][time_index][0], sentences[video_index][1][time_index][1]); repeat++; if (repeat >= 5) { repeat = 0; time_index++; if (time_index >= sentences[video_index][1].length) { time_index = 0; video_index++; } } }
<a href="#" onclick="myLoop()" > Play </a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.