How to compare two array lengths?

How to compare two array length and to perform the function? There are two arrays if the two arrays of length more than one that executes a function, if some of them length becomes exactly equal to 0 to stop !!! And it all has to work in a setInterval, that would change if you practiced one or another function. How this can be done?

function start() {
document.getElementById('ok').innerHTML = '<div>Length of two arrays is greater than 0</div>'
//Function to run
}
function stop() {
document.getElementById('ok').innerHTML = '<div>Full of arrays is 0</div>'
// Stop running the function if any of the counters becomes equal to "0" 
//and wait for the changes if both the counter equals more than 0 
}

liki function() {
 const ids = [...document.querySelectorAll('#user uidm')].map(uidm => uidm.attributes.uid.value);
 const idss = [...document.querySelectorAll('#user uid')].map(uid => uid.attributes.uid.value);
 if (ids.length > 0 && idss.length > 0) {
start();
 } else {
stop();
}
}}
March 19th 20 at 09:06
2 answers
March 19th 20 at 09:08
Solution
The decision
liki function() {
 const ids = [...document.querySelectorAll('#user uidm')].map(uidm => uidm.attributes.uid.value);
 const idss = [...document.querySelectorAll('#user uid')].map(uid => uid.attributes.uid.value);
 if (ids.length > 0 && idss.length > 0) {
 status = 'on'
 }else {
 status ='off'
}
 if(status==='on') {
start();
}
}
setInterval(liki,100);
 let timer = 1000;
 let timenull = null;
 function start() {
 if (!timenull) {
console.log('running')
}
clearTimeout(timenull);
 timenull = setTimeout(stop, timer);
}
 function stop() {
 timenull = null;
 console.log('Not running');
 }
March 19th 20 at 09:10
//and wait for the changes if both the counter equals more than 0

so equal or more?

and the problem is to put all of this in setInterval?
let one = [...document.querySelectorAll('#user uidm')].map(uidm => uidm.attributes.uid.value);
let two = [...document.querySelectorAll('#user uid')].map(uid => uid.attributes.uid.value);
let ok = document.getElementById('ok');

function start() {
 ok.innerHTML = '<div>Length of two arrays is greater than 0</div>'
}

function stop() {
 ok.innerHTML = '<div>Full of arrays is 0</div>'
}

setInterval(() => {
 if (one.length > 0 && two.length > 0) {
start();
 } else {
stop();
 } 
}, 1000);
and the problem is to put all of this in setInterval?


This code for example. Now I will try to explain the meaning.

On the page there is a special room where can stay 4 people, two on one side two on the other. On the one hand there may be one or two people but not the other one. The process is so among users there are test questions that need to load when on two sides by one or more users. But since one can leave or enter other for example have a block with questions to clean!!! Why setInterval does not work because the block will be inserted permanently. You can of course using CSS property to make but there is a problem. Test which is loaded in the unit will proceed in the background so to speak and the result is not correct. - Brock.McCullough commented on March 19th 20 at 09:13
@Brock.McCullough, in this case, you should use events to add and remove user and in the handler check the length of the array - estefania_Renner commented on March 19th 20 at 09:16
@estefania_Renner,

With the YouTube player did that!!! But there is one thing where you can track the event of the player!!! Here is the code as on YouTube did

function playVideo(){
 document.getElementById('pl').innerHTML="<div id='pl></div>";
 var music = window.IDvideo;
 var volume = player.getVolume();
player.loadVideoByUrl(music);
player.playVideo();
document.getElementById('range').value=volume;
 $('#range').on('change', function () 
{player.setVolume($(this).val());});
 $('#mute-toggle').on('click', function()
 {var mute_toggle = $(this);
if(player.isMuted()){
player.unMute();
mute_toggle.text('volume_up');
 } else{player.mute();
mute_toggle.text('volume_off');}});
 function stopPlay() {
 var play = player.getPlayerState();
 if (play === 0) {
$.ajax({
 url: "roomUser/selectplay.php",
 type: "POST", 
 data: {playadd: window.t}, 
 dataType: "JSON",
 success: function send(audiolist) {
 if (!Array.isArray(audiolist) || !audiolist.length) {
 document.getElementById('pl').innerHTML = "<div id='pl' style='width: 260px;height: 150px;position: absolute;margin-left: 741px;background: black'>" +
 "<input id='add' type='button' value='Add video' name='addTrack' OnClick='addTrack();'>" +
 "</div>";document.getElementById('ok').innerHTML = '<div>' +
'</div>';
}else{
 var selectdata = audiolist[0];window.IDvideo = "https://www.youtube.com/v/"+selectdata.url+"?version=3";playVideo()
}}});
 setTimeout(function() {
clearInterval(timerPlayV);
 }, 100);}}
 var timerPlayV = setInterval(function() {
stopPlay();
 }, 1000);}
- Brock.McCullough commented on March 19th 20 at 09:19
@Brock.McCullough, you add/remove a user is accompanied by what? - estefania_Renner commented on March 19th 20 at 09:22
@estefania_Renner, There's a lot of room system. When adding user data is sent in a websocket and user see in the list!!! If the leaves also go data!!! But the array can have multiple users!!! And for this I track the attribute. When the user comes! - Brock.McCullough commented on March 19th 20 at 09:25
@Brock.McCullough, well before sending data and check - estefania_Renner commented on March 19th 20 at 09:28

Find more questions by tags JavaScript