How do I increase the array value by one each time I click the js button?

Asked 2 years ago, Updated 2 years ago, 134 views

How do I change the track by increasing the track index one by one each time I click the button?

When executing the code below, the track will be changed only if all labels are Member7 and the rest will be false.

var tracks = video.videoTracks;
testbutton.onclick = () => {
    for (i = 0; i < tracks.length; i++) {
        if (tracks[i].label === "Member7") {
            tracks[i].selected = true;
        } } else {
            tracks[i].selected = false;
        }
    }
};
VideoTrackList {0: VideoTrack, 1: VideoTrack, 2: VideoTrack, 3: VideoTrack, 4: VideoTrack, 5: VideoTrack, 6: VideoTrack, 7: VideoTrack, length: 8, selectedIndex: 0, onchange: null, onaddtrack: null, onremovetrack: null, …}

It's a video track.

video javascript

2022-09-20 16:23

1 Answers

You are specifying something like "Track7" in the code, but there is no answer at this rate. Who knows if there will be more than 7 tracks or less?

So what you're going to do now is make the first one, the second one on the given playlist, and the third one, the second one... And if you're going to make the first one the last one, I'll approach it with the shift concept. If you look at how shift() in the JS Array works, you'll see that the code below makes sense.

If it's in the opposite direction, look for unshift. Anyway, this approach has the advantage of mechanically circulating the entire array, so you don't have to hardcode things like "Track7". Look into it!


2022-09-20 16:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.