简体   繁体   中英

How to check and uncheck a checkbox infinitely?

I have this code, I tried with the setTimeout, promises and callback functions with no luck.

document.querySelectorAll("input").forEach((el, i) => {
    setTimeout(() => { 
        el.checked = true 
    }, i * 300);
})

Fiddle

Any help is welcome.

The setup() function sets a timeout on each checkbox. When the last one fires, it sets them all up again:

function setup() {
  document.querySelectorAll("input").forEach((el, i) => {
    setTimeout(() => { 
      el.checked = ! el.checked
      if ( i+1 == document.querySelectorAll("input").length) {
        setup()
      }
    }, i * 300);
  }
)}

setup()

https://jsfiddle.net/2u9rqot1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM