How to compare two array lengths and execute a function?

There are two arrays, if in two arrays the length is more than one, then the function is executed, if in some of them the length becomes equal to 0, stop the running !!!

How can this be done?

function start() { document.getElementById('ok').innerHTML = '<div>Длина двух массивов больше 0</div>' //Функция запуска } function stop() { document.getElementById('ok').innerHTML = '<div>Какойто из массивов равен 0</div>' // Остановить запущенную функцию если какой-то из счетчиков стал равен "0" //и ждать изменения если оба счётчика станут равны больше "0" } function liki() { const ids = [...document.querySelectorAll('#user uidm')].map(uidm => uidm.attributes.uid.value); if (ids.length > 0) {} const idss = [...document.querySelectorAll('#user uid')].map(uid => uid.attributes.uid.value); if (idss.length > 0) {} } 
  • How to understand остановить запущенную ? - Air
  • Frankly, it is not entirely clear what exactly you want to implement. Maybe someone else will understand - Air
  • At what event will this happen? Describe the question in more detail. - Air

2 answers 2

I'm not sure that I understood the author's question correctly, but at the level of intuition, I think the solution is ...

 function start() { document.getElementById('ok').innerHTML = '<div>Длина двух массивов больше 0</div>' //Функция запуска } function stop() { document.getElementById('ok').innerHTML = '<div>Какойто из массивов равен 0</div>' // Остановить запущенную функцию если какой-то из счетчиков стал равен "0" //и ждать изменения если оба счётчика станут равны больше "0" } function liki() { 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 (idss.length > 0 && ids.length > 0) { stop(); } else { start(); } } 
  • Yes!!! Yes!!! Comparison all right, thanks! It is only necessary to check whether the state of one of the arrays has changed or not !!!! I will briefly explain there a game for 4 users on the one hand, two players and on the other !!! If on both sides, one by one, the game starts !!! If there are zero players on any side, the game stops !!! If someone came, then it starts again !!! As it is necessary that in a cycle it would all be !!! That's what this comparison is for !!! - Local Devil
  • Well, as far as I understand, you need on the server side, to check whether someone came into the game or not. The question is very general ... - Air

The solution to this issue

 function liki() { 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('запущенно') } clearTimeout(timenull); timenull = setTimeout(stop, timer); } function stop() { timenull = null; console.log('Не запущенно'); }