简体   繁体   English

如何使用 javascript 遍历这个数组?

[英]How can I loop through this array using javascript?

Given this script:鉴于此脚本:

<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>

Now I want to fill in myLoop function.现在我要填写myLoop function。

I want to loop each pairs 5 times before jump to the next pair in the array.我想在跳转到数组中的下一对之前循环每对 5 次。

For example, -the first pair: 'journey27.mp4', loop " [1, 2] " 5 times例如,-第一对:'journey27.mp4',循环[1, 2] ”5次

then the next pair: 'journey27.mp4', loop " [5, 7] " 5 times然后是下一对:'journey27.mp4',循环[5, 7] ”5 次

then the next pair: 'journey28.mp4', loop " [10, 12] " 5 times然后是下一对:'journey28.mp4',循环[10, 12] ”5 次

then the next pair: 'journey28.mp4', loop " [13, 17] " 5 times然后是下一对:'journey28.mp4',循环[13, 17] ”5 次

and so on等等

Note: The playVideo function above works注意:上面的playVideo function 有效

How can I loop through this array using javascript?如何使用 javascript 遍历这个数组?

Use global variables to hold the current indexes and repetition count for the video.使用全局变量来保存视频的当前索引和重复次数。 Increment the repetition count, and when reaches 5 increment the indexes.增加重复计数,当达到5时增加索引。

There's no loop, the iteration happens when the user clicks the link.没有循环,迭代发生在用户单击链接时。

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM