I am calling a function on scroll, when my purpose is served I am unbinding the scroll.But on the click of menu tab I want to bind the scroll again.Cant we re-bind the scroll after unbinding it with out refreshing the page.Here is my code.
var currentPage = 1;
var xhr = null;
var flag = 0;
(window).bind('scroll');
$(document).ready(function()
{
$('#gallery').bind("click",function()
{
flag = 0;
currentPage = 1;
alert(currentPage);
scroll()
});
});
function scroll(){ $(window).bind('scroll');}
function refresh(){flag = 1; alert("flag");}
function checkScroll() {
if (flag==1){ $(window).unbind('scroll');}
if(nearBottomOfPage() == 0)
{
currentPage ++;
xhr = $.ajax(
{
url : '/ideabank?page=' + currentPage,
beforeSend: function() {
$('#loading').show()
},
complete: function(){
$('#loading').hide()
},
success : function(){}
} );
}
}
function nearBottomOfPage() {
return scrollDistanceFromBottom();
}
function scrollDistanceFromBottom(argument) {
return $(document).height() - ($(window).height() + $(window).scrollTop());
}
$(window).bind('scroll',function (){
checkScroll();
});
You can rebind a handler for the scroll event at any time, but two out of your three calls to .bind('scroll')
are not correct because they don't supply a handler function to bind. The statements:
$(window).bind('scroll');
function scroll(){ $(window).bind('scroll');}
...need to be like this:
$(window).bind('scroll',function (){
checkScroll();
});
// OR you don't actually need the anonymous function,
// you can bind checkScroll directly:
$(window).bind('scroll',checkScroll);
// AND
function scroll(){ $(window).bind('scroll',checkScroll);}
(Note also that on line 4 you're missing the $
from the start of (window).bind('scroll')
, but I assume that's just a typo in the question not in your real code.)