javascriptjqueryinfinite-scrolltwitter-flight

Scroll Event Causes Custom Event To Trigger Twice (should be once)


Following function has the scroll event attached, which then triggers the custom event defined on line 5.

Line 5 seems to be causing the function to invoke twice (if removed line 4 is printed once, with line 5 its twice).

Custom event should only fire once, at the moment its twice.

 this.on(window, 'scroll', function(event){
    var win = $(window);
    if ($(document).height() - win.height() === win.scrollTop()) {
        console.log('testing 123');
         this.trigger('uiHandlRequest', { type: 'foo' });
        return false; 
        }            
   });

Solution

  • Use 'window' instead of 'this' while triggering custom event.

    this.on(window, 'scroll', function(event){
        var win = $(window);
        if ($(document).height() - win.height() === win.scrollTop()) {
            console.log('testing 123');
             window.trigger('uiHandlRequest', { type: 'foo' });
            return false; 
            }            
       });