Click here to Skip to main content
15,908,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a function which does a number of things, to show you what's important for this question, see here:

JavaScript
function SIP(config) {
const videoSource  = document.getElementById('streamVideo');
}


I have a function above that takes a config file and creates a session with it, an plays a video file to that html ID. I have loop like so:

JavaScript
var config = '10000';

if (config) {
	while (config < 10011) {
      fetch('data/' + config + '.json')
        .then(response => response.json())
        .then(data => {
          window.app = new SIP(data);
          console.log('fetch config', config, data);
	      config++;
	      //var ifyes = true;
          return;
        }).catch(console.error);
    }


What I want to happen is every time the loop is iterated over, it instead plays the video file in an incremented ID so it plays in not the same HTML element as the first iteration but the next ID (in this case, streamVideo2).

What I have tried:

A number of things, a do/while loop and I have tried building another file with variables relating to the ID of the HTML elements I'm trying to play a video to, the issue is that the function wants to always play to streamVideo and without changing the function manually I cannot play to another HTML element - there must be a way to do this
Posted
Comments
Richard Deeming 13-Dec-21 9:01am    
You'll need to change the function so that you pass in the ID of the element you want to use as a parameter.
mc96 13-Dec-21 9:07am    
HI Richard thanks for that I forgot to mention I have tried that,

I can change:

function SIP(config) to function SIP(config, videonum)

and then

window.app = new SIP(data, video);

in the loop, but than I have the issue of how can I pass a different ID to the function everytime?
Richard Deeming 13-Dec-21 9:09am    
window.app = new SIP(data, config);

Then use the video number passed in as the second parameter to construct the ID of the element you want to use. Eg:
const videoSource  = document.getElementById(`streamVideo${videonum}`);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900