chtefi
7/27/2015 - 10:00 AM

An implementation of requestAnimationFrameOnce

An implementation of requestAnimationFrameOnce

function requestAnimationFrameOnce(fn) {
    var requested = false;
    return function(lastState) {
        if (!requested) {
            requested = true;
            requestAnimationFrame(function() {
                fn(lastState);
                requested = false;
            });
        }
    }
}

// Usage
// It will take the last param given when the callback will be called by rAF
// here: scrollTop
function onScroll(cb) {
    return function() {
        var scrollTop = viewport.scrollTop();
        requestAnimationFrameOnce(cb)(scrollTop);
    };
}