簡體   English   中英

如何使用 javascript 遍歷這個數組?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM