Search code examples
jqueryjquery-waypoints

Executing a function only once


I'm using Waypoints.js to start a counter function when I scroll down the page:

$(".counter").waypoint(function() {
  $(".counter").count();
}, {offset:"100%"});

Everything works great but if I scroll up the page and down again to the counter, it starts counting all over again. How can I execute the counter function only once?


Solution

  • Not sure what that code is meant to do, but you can always use a flag variable to control something like that:

    var executed = false;
    $(".counter").waypoint(function() {
        if (!executed) {
            executed = true;
            $(".counter").count();
        }
    }, {offset:"100%"});