Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

addEventListener works twice instead of one

Im doing carousel and suddenly addEventListener(‘animationend’, function(){}) running twice. I placed console.log to view "currentItem", and at the third time it running twice. Where is my mistake?
https://codepen.io/sundozer/pen/VwxENKw

squares[currentItem].addEventListener('animationend', function(){
        console.log(currentItem)
        squares[currentItem].classList.remove('toLeft')
        squares[((currentItem+1)+squares.length) % squares.length].classList.remove('fromRight')
        squares[currentItem].classList.add('hide')
        changeCurrentItem(currentItem+1)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

In your example, whenever you click the button you are running a function which adds new event listeners. If you do not remove the event listeners then they will begin to stack. You can deal with this by saving a reference to the function you add and removing them after. I have fixed your codepen with the following code:

const butR = document.querySelector('.butR')
const squareRed = document.querySelector('.squareRed')
const squarePink = document.querySelector('.squarePink')
const squareBlue = document.querySelector('.squareBlue')
const squares = document.querySelectorAll('.squares')
const container = document.querySelectorAll('.container')
let currentItem = 1;
let enable = 1;

const changeCurrentItem = (number) => {
    currentItem = number % squares.length;
}

butR.addEventListener('click', function(){
    if (enable === 1) {
        squares[currentItem].classList.add('toLeft');
        squares[((currentItem+1)+squares.length) % squares.length].classList.add('fromRight');
        squares[((currentItem+1)+squares.length) % squares.length].classList.remove('hide');

        const animStart = function() {
            enable = 0;
        }

        const animEnd = function() {
            enable = 1;
            console.log(currentItem);
            squares[currentItem].classList.remove('toLeft');
            squares[((currentItem+1)+squares.length) % squares.length].classList.remove('fromRight');
            squares[currentItem].classList.add('hide');
            squares[currentItem].removeEventListener('animationstart', animStart);
            squares[currentItem].removeEventListener('animationend', animEnd);
            changeCurrentItem(currentItem+1);
        }

        squares[currentItem].addEventListener('animationstart', animStart);
        squares[currentItem].addEventListener('animationend', animEnd);
    }
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading