javascript – how to stop scroll event after completion

Question:

I ran into a problem so that when scrolling down on the page, elements appear, after they have already appeared, the dosihpor script works, with each scroll it continues to add the active class as you can stop it. js:

$(window).scroll(function() {
$(".advantage-options").each(function() {
            var pos = $(this).offset().top;
            var winTop = $(window).scrollTop();
            if (pos < winTop + 780) {
                var i = 0;
                var interval = setInterval(function() {
                    var cont = document.getElementsByClassName("adv-item");
                    if (i < cont.length) {
                        cont[i].className += " active";
                        i++;
                    } else {
                        clearInterval(interval);
                    }
                }, 120);
            }
        });
});

Answer:

Check:

if(cont[i].className.indexOf("active")==-1){

   cont[i].className += " active";

}

Or use jQuery, since you still use:

$(".adv-item").addClass("active");

Same option with jQuery, but with element number:

$(".adv-item").eq(i).addClass("active");
Scroll to Top
AllEscort