Call the javascript function only once per click event or scroll event for a specific class

Let's say I have a javascript function call function playGauge().

I want to call this function only once when either a divc class="myClass"is pressed or scrolled. For a click, I use the following code:

    var myclassClicked = false;
    $('.myClass').click(playGauge);


    function playGauge(){
    if (myclassClicked === true) return false;
    myclassClicked = true;
///do some other stuff
}

How to add scrolling functionality is similar to my code and mine function playGauge().

Thank!

+3
source share
3 answers

$.one is your friend:

$('.myClass').one('click scroll', function(event){ ... });
+3
source

Remove the event handler after the event occurs on any element matching the selector

$('.myClass').on('click scroll', function() {
    $('.myClass').off('click scroll')
    playGauge();
});
+3
source

:

$('.myClass').scroll(playGauge);

0

All Articles