dimanche 13 décembre 2020

Running a function using setInterval and clearInterval when conditions are met?

I am not sure if this is the best way to do this or even if I am doing this right. I have a toggle switch that can be on and off, so when it is on it runs the function autoAdmit() once in a while and when it's switched off then it should not be running it. How should I do this? I don't know what's the best way to do this so I tried maybe the dumbest way to go at it..

function autoAdmit() {
    for (let element of document.getElementsByTagName('span')) {
        if (element.innerHTML === 'Admit') {
            element.click();
            }
        }
        
  
      
} 


document.addEventListener('DOMContentLoaded', function() {
    var checkbox = document.querySelector('#auto-admit .mdc-switch__native-control');

    function isChecked() {
      if (checkbox.checked) {
        
        clearInterval(autoAdmit)
        setInterval(autoAdmit, 1000)

      } else {
    
        clearInterval(autoAdmit)
        setInterval(autoAdmit, 10000000000000)
        
      }
    }
    checkbox.addEventListener('change', function() {
      isChecked();
    });
  }

);



Aucun commentaire:

Enregistrer un commentaire