When the page loads, the script for the background stripes runs fine.. the stripes appear then begin to fade out after a certain amount of stripes is in the array.
As long as the browser tab is selected(focus) the script runs as expected. However, If you open a New browser tab and select the new tab. The script for the stripes continues to run in the background on the original tab but somehow ignores the condition and the stripes pile up to infinity. When switching back to the original tab you then see the ugly pile up.
Why is this happening?
https://codepen.io/trentHarlem/full/yLVvvav
const addButton = document.getElementById('add-stripe');
const guitar = document.getElementById('guitar');
function addStripe() {
let qsaSpan = document.querySelectorAll('span');
let stripe = document.createElement('span');
let stripeArray = Array.from(qsaSpan);
let color = 'white';
let z = Math.floor(Math.random() * 2);
if (Math.random() > 0.65) {
color = 'black';
}
let angle = Math.floor(Math.random() * 360);
let x = Math.floor(Math.random() * 30) - 15;
let y = Math.floor(Math.random() * 30) - 15;
let width = Math.floor(Math.random() * 6) + 1;
// console.log('click', color, angle, x, y, width);
stripe.style.backgroundColor = `${color}`;
stripe.style.zIndex = `${z}`;
stripe.style.transform += `translateX(${x}%)`;
stripe.style.transform += `translateY(${y}%)`;
stripe.style.transform += `rotate(${angle}deg)`;
stripe.style.height = `${width}%`;
guitar.insertAdjacentElement('beforeend', stripe);
// console.log('length', stripeArray.length);
function parRemove() {
stripeArray[0].remove()
stripeArray[0].removeEventListener('transitionend', parRemove)
stripeArray[0].removeEventListener('webkitTransitionEnd', parRemove)
}
if (stripeArray.length > 5) {
stripeArray[0].classList.add('fall');
stripeArray[0].classList.add('fade');
// console.log(' stripeArray[0].classList', stripeArray[0].classList.value);
stripeArray[0].addEventListener('transitionend', parRemove, false);
stripeArray[0].addEventListener('webkitTransitionEnd', parRemove, false);
}
else if (stripeArray.length < 5) {
}
}
const stripeTimer = setInterval(addStripe, 2000);