0

I am creating randomly fading squares on the image. They end up one by one partially revealing the image. This function was meant to hide random squares one by one but it is hiding them abnormally.

setInterval(disappearingBoxes, 500);

function disappearingBoxes() {
  for (let i = 0; i < boxes.children.length; i++) {
    i = Math.floor(Math.random() * 16).toFixed()
    if (!boxes.children[i].classList.contains('hidden') && boxes.children[i].id === i) {     
      boxes.children[i].removeAttribute('id');      
      if (boxes.children[i].getAttribute('id') === null) {
        boxes.children[i].classList.add('hidden') 
      } else {
        let newI = Number(Math.floor(Math.random() * 16).toFixed());
        boxes.children[newI].classList.add('hidden')    
      }  
    }

1 Answer 1

0

Try This

function disappearingBoxes() {
  for (let i = 0; i < boxes.children.length; i++) {
    if (!boxes.children[i].classList.contains('hidden') && boxes.children[i].id === i) {     
      boxes.children[i].removeAttribute('id');      
      if (boxes.children[i].getAttribute('id') === null) {
        boxes.children[i].classList.add('hidden') 
      } else {
        let newI = Number(Math.floor(Math.random() * 16).toFixed());
        boxes.children[newI].classList.add('hidden')    
      }  
    }
  }
}

Sign up to request clarification or add additional context in comments.

1 Comment

this is the same as before, still squares disappearing abnormally

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.