[英]How to loop over again an array when it finishes? JavaScript
Here is my code:这是我的代码:
useEffect(() => {
playerRef.current.seekTo(convertedflat[currentTimeIndex], 'seconds');
});
return (
<>
<main>
<div className={style.main_container}>
<NavBar />
<div className={style.heronext}>
<div className={style.hero_container}>
<ReactPlayer
ref={playerRef}
playing
controls={true}
url={`videos/${episList[currentEpisodeIndex]}.mkv`}
width="90%"
height="55rem"
/>
</div>
<button
className={style.next}
onClick={() => {
setCurrentTimeIndex((prevTimeIndex) => prevTimeIndex + 1) %
convertedflat.length;
setcurrentEpisodeIndex((prevTimeIndex) => prevTimeIndex + 1) %
convertedflat.length;
console.log(currentTimeIndex);
console.log(currentEpisodeIndex);
}}
>
Next
</button>
basically when you click on the next button it moves to the next timestamp until the array finishes.基本上,当您单击下一个按钮时,它会移动到下一个时间戳,直到数组完成。 but I want to it starts again when the array list is finished.Currently when the array navigation is finished it shows error.但我希望它在数组列表完成后重新启动。当前当数组导航完成时它显示错误。
I used % length but it doesnt work.我使用了 % 长度,但它不起作用。 Here is the error message when the list is finished:这是列表完成时的错误消息:
TypeError: Failed to set the 'currentTime' property on 'HTMLMediaElement': The provided double value is non-finite. TypeError:无法在“HTMLMediaElement”上设置“currentTime”属性:提供的双精度值是无限的。
You should be careful with these lines你应该小心这些行
setCurrentTimeIndex((prevTimeIndex) => prevTimeIndex + 1) % convertedflat.length; setCurrentTimeIndex((prevTimeIndex) => prevTimeIndex + 1) % convertflat.length;
I think there is a problem with brackets.我认为括号有问题。 It should be something like;它应该是这样的;
setCurrentTimeIndex((prevTimeIndex) => (prevTimeIndex + 1) % convertedflat.length); setCurrentTimeIndex((prevTimeIndex) => (prevTimeIndex + 1) % convertflat.length);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.