0

I have a class which I call via:

this.infiniteScroll = new gd.InfiniteScroll();

In this class it checks if user is at bottom of window.

Later on in my script I have no use for this infinite scroll script (as all data has been loaded). How can I delete it? or stop it from checking if user is at bottom of window?

Here's the infinite scroll class:

(function(){
"use strict";

var InfiniteScroll = function() {
    this.init();
};

var p = InfiniteScroll.prototype = gd.BaseClass.extend(gd.BaseClass);
p.BaseClass_init = p.init;

/*
 * Public properties
 */
p.canLoad = true;
p.cog;

/* 
 * Public methods
 */
p.init = function() {
    // Super
    this.BaseClass_init();

    // Init
    this.ready();

};

p.ready = function() {

    this._initInfiniteScroll();
};

p.loadRequested = function(){

    p.canLoad = false;
    console.log('show cog');
    $.event.trigger('loadRequested');

}

p.loadComplete = function(){

    p.canLoad = true;
    console.log('hide cog');
    console.log(p.canLoad);
}

p._initInfiniteScroll = function() {

    $(window).scroll(function(){  
        console.log('scroll!');
        if(($(window).scrollTop() == ($(document).height() - $(window).height())) && p.canLoad){

            p.loadRequested();

        }  
    });   

}


gd.InfiniteScroll = InfiniteScroll;
}(window));
panthro
  • 22,779
  • 66
  • 183
  • 324

3 Answers3

2

Usually you could just delete this.infiniteScroll. But I assume that in this case it actually attaches some event handlers, which means this isn't enough.

If the library is written well, you will probably have a method to detach all event handlers, a desctructor function or something similar.

Munter
  • 1,079
  • 5
  • 7
0

Just give your class a "deactivate" method that you can invoke as required:

p.deactivate = function() {
    $(window).off('scroll');
};

If you're utterly finished with the object you may also delete this.infiniteScroll to allow the garbage collector to reclaim the memory, but given how little memory this class takes there's little benefit in doing so.

NB: ideally you should "namespace" your event handlers so that you can deactivate your handlers independently of any others, e.g.:

$(window).on('scroll.infinite', ...);

and then subsequently:

$(window).off('scroll.infinite');
Alnitak
  • 334,560
  • 70
  • 407
  • 495
0

Without modifying the plugin code, you can probably just do:

this.infiniteScroll.canLoad = false;

EDIT:

Now that I know it's your own code, you can simply keep track of your listener function and provide a destruction mechanism that will remove the listener.

p._initInfiniteScroll = function() {

    $(window).scroll(this._scrollHandler = function(){  
        console.log('scroll!');
        if(($(window).scrollTop() == ($(document).height() - $(window).height())) && p.canLoad){

            p.loadRequested();

        }  
    });   

}

p.destroy = function () {
    $(window).off('scroll', this._scrollHandler);
    //you should probably call the destruction function of the parent class
    //if there's any.
};

Then just call this.infiniteScroll.destroy();

plalx
  • 42,889
  • 6
  • 74
  • 90
  • Yes, but then it will still check for this everytime the user scrolls. – panthro Feb 06 '14 at 13:59
  • 1
    @panthro True, but there's no other way without modifying the plugin. – plalx Feb 06 '14 at 14:00
  • It's not a plug in. It's code ive written so can mod it all I want. – panthro Feb 06 '14 at 14:01
  • @plalx I don't believe that's valid syntax for `.off` - you have to specify the event name, and (optionally) the event handler reference. – Alnitak Feb 06 '14 at 14:08
  • 1
    @Alnitak Yeah I corrected it already, careless mistake. It certainly did not deserve a downvote... – plalx Feb 06 '14 at 14:15
  • @plalx I didn't give you one, and if you're the one that downvoted my accepted answer because you believe I did then that's very petty. – Alnitak Feb 06 '14 at 14:23
  • That's natural, I got downvoted and my answer is similar to an upvoted one :P – SparK Feb 06 '14 at 19:46