d4nyll / lethargy

Distinguish between scroll events initiated by the user, and those by inertial scrolling
http://d4nyll.github.io/lethargy/
MIT License
556 stars 47 forks source link

_this.lethargy.check(e) must be called once only #5

Closed ambroisemaupate closed 9 years ago

ambroisemaupate commented 9 years ago

On Chrome (Windows) and with some speedy mouses (gamers one), lethargy stops to work because we called _this.lethargy.check(e) several times in the same function.

You should update your example to tell people to store _this.lethargy.check(e) value in a temp variable instead of using it more than on time.

Bad example, _this.lethargy.check(e) value can change between calls:

/**
 * Mousewheel
 * @return {[type]} [description]
 */
Page.prototype.onMousewheel = function(e){
    var _this = this;

    e.preventDefault();
    e.stopPropagation();

    if(_this.lethargy.check(e) !== false) {
        console.log('Lethargy');
        console.log(_this.lethargy.check(e));

        if (_this.lethargy.check(e) == 1 && _this.blockIndex > 0) {
            _this.switchBlock('up');
        }
        else if(_this.lethargy.check(e) == -1 && _this.blockIndex < _this.blockLength-1){
            _this.switchBlock('down');
        }
    }
};

Fixed example

/**
 * Mousewheel
 * @return {[type]} [description]
 */
Page.prototype.onMousewheel = function(e){
    var _this = this;

    e.preventDefault();
    e.stopPropagation();

    var check = _this.lethargy.check(e);

    if(check !== false) {
        console.log('Lethargy');
        console.log(check);

        if (check == 1 && _this.blockIndex > 0) {
            _this.switchBlock('up');
        }
        else if(check == -1 && _this.blockIndex < _this.blockLength-1){
            _this.switchBlock('down');
        }
    }
};
d4nyll commented 9 years ago

Thanks @ambroisemaupate I will keep this in mind when I work on lethargy towards the end of the month!