Ancinek Ancinek - 7 months ago 36
Javascript Question

animationEnd event handler - event heard two times

I have a code:

$('#sectionName').on('animationend webkitAnimationEnd oAnimationEnd', function () {
alert("Hello");
});


which is meant to trigger the alert when animation of #sectionName ends.

It does its job but it is also triggered first time the website is launched.

Is there a way to prevent it? Some way to trigger this alert only when the animationend event is heard for the second time?

PS I am using wow.js, so the #sectionName object slides in, after scrolling to certain page point, and then I want the alert to be displayed.

Answer

Try it simple:

Var firstTime = true
$('#sectionName').on('animationend webkitAnimationEnd oAnimationEnd', function () {
    if(firstTime){
        firstTime = false;
        return;
    }
    alert("Hello");
});
Comments