Skip to content
This repository has been archived by the owner on Mar 27, 2019. It is now read-only.

Uncaught TypeError: Cannot read property 'length' of undefined (if you enable scrollbars) #768

Closed
ghost opened this issue Aug 5, 2014 · 8 comments
Assignees

Comments

@ghost
Copy link

ghost commented Aug 5, 2014

If I have hideScrollbar:false and scrollbars:true as an option and after a resize I reinitialise the iScroll (so I destroy it first and reinitialise it) with the same option I get this error if the scrollbars are true as soon as I switch this options to false the error doesn't happen but it does every time if I have the scrollbar options enabled as above.

@mr-moon
Copy link

mr-moon commented Aug 14, 2014

Yeah, this happens prior to execution of scrollEnd event.

Uncaught TypeError: Cannot read property 'length' of undefined
    _indicatorsMap
    (anonymous function)
    IScroll._execEvent
    (anonymous function)

And if we look at what triggers it:

// Execute the scrollEnd event after 400ms the wheel stopped scrolling
clearTimeout(this.wheelTimeout);
this.wheelTimeout = setTimeout(function () {
    that._execEvent('scrollEnd');
    that.wheelTimeout = undefined;
}, 400);

It looks like this error raises when you have destroyed the iScroll object, but yet timer has not been cleared out.

@nim23
Copy link

nim23 commented Aug 28, 2014

I have the same problem when destroying and re-initialising IScroll. However the code that's causing the problem for me is the resize function.

 _resize: function () {
    var that = this;
    clearTimeout(this.resizeTimeout);
    this.resizeTimeout = setTimeout(function () {
        that.refresh();
    }, this.options.resizePolling);
}

As far as I can tell that.refresh() tries to update the scrollbar indicators and because it has already been removed from the DOM and from the IScroll object, IScroll throws an error. A simple check on the below function fixes the problem

function _indicatorsMap(fn) {
        if (that.indicators) { //check if indicators exist
            for (var i = that.indicators.length; i--;) {
                fn.call(that.indicators[i]);
            }
        }
    }

I think it's sensible to add the check, as I have seen on the other places IScroll does perform a check for the indicators.

@littletinyfish
Copy link

I can confirm that nim23's fix worked for me.

@rosko
Copy link

rosko commented Feb 13, 2015

Hello. I got the same error when it tries to continue scrolling on inertia after destroying iScroll.

@benlwilliams
Copy link

nim23's fix worked for me as well.

@haskelcurry
Copy link

In case if someone is facing this issue and don't want to modify the source code of the lib, you can do this before the destroy:

scroll._events = {};

@rwhepburn
Copy link

+1 Experienced the same issue when using iScroll with Knockout JS.

sculove added a commit that referenced this issue Mar 30, 2016
@sculove
Copy link
Collaborator

sculove commented Mar 30, 2016

@nim23 thank you. I merged on master branch.

@sculove sculove self-assigned this Apr 1, 2016
@sculove sculove closed this as completed Apr 1, 2016
aisouard pushed a commit to Mappy/iscroll that referenced this issue Feb 22, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants