gpt4 book ai didi

Javascript暂停循环直到事件发生

转载 作者:行者123 更新时间:2023-11-28 00:13:10 25 4
gpt4 key购买 nike

我有 javascript 问题,这是我的代码的一部分:

for(var a = 0; a < videos.length; a++) {
source.setAttribute('src', videos[a]);
video.load();
video.play();

for(var b = 0; b < times[a].length; b++) {
video.addEventListener("timeupdate", function() {
if (this.currentTime >= times[a][b]) {
this.pause();
var answer = prompt("Please answer the question", "Answer");
}
}, false);
}
}

在数组videos中,我有视频链接,在数组times中,我有时会以不同的方式暂停每个视频的视频。

我的目标是开始一个视频,然后在特定时间暂停视频并在回答问题后恢复,然后当视频结束时开始另一个视频并再次提问。我的 for 循环立即完成所有操作,但它没有按我想要的方式工作。我想出了两个解决方案:

  1. 暂停循环,直到到达事件。
  2. 使用事件监听器来完成此操作。

我不知道该怎么做,所以我请求你的帮助。

最佳答案

您应该删除 for 循环并仅处理事件。像这样的事情至少应该可以解决一些问题:

    var a = 0;
video.src = videos[a];
video.load();
video.play();
check_time();

//instead of looping through your video, you wait until one is finished
//to play the next one.
video.addEventListener("ended", function(){
a++;
if(a<videos.length){
video.src = videos[a];
check_time();

}
//this will add event listener each time video is changed.
function check_time(){
var b = 0;
video.removeEventListener("timeupdate")
video.addEventListener("timeupdate", function() {
if (this.currentTime >= times[a][b]) {
this.pause();
var answer = prompt("Please answer the question", "Answer");
b++;
}
}, false);
}

关于Javascript暂停循环直到事件发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30698965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com