I'm trying to make a simple web app for audiobooks listening. And I'm trying to set a timeline length. I wanted to do it by summing up duration of all chapters. But when I do it this way:
let timelineLength = 0;
let chapterLength;
currentPart.forEach(chapter => {
chapterLength = chapter.duration;
timelineLength += chapterLength;
});
chapterLength is NaN so timelineLength is also NaN. When I type chapters[index].duration in chrome console after the app's been loaded it works just fine. I have no idea what I'm doing wrong.
This worked:
currentPart.forEach(chapter => {
chapter.addEventListener('loadeddata', () => {
chapterLength = chapter.duration;
timelineLength += chapterLength;
});
});
I had to wait for the data to load.