Global variables do not change their values. Specifically pageNumber remains 1 and it is supposed to be incremented in a function. I try to implement continious scroll and get next block of code, using post query to action in my asp.net mvc controller, but it of course it always gets me the same result
<script type="text/javascript">
pageNumber = 1; //Infinite Scroll starts from second block
noMoreData = false;
inProgress = false;
function ScrollFunction(pageNumber, noMoreData, inProgress) {
var documentHeight = $(document).height();
var windowHeight = $(window).height();
var windowScrollTop = $(window).scrollTop();
var documentMinusWindowHeight = documentHeight - windowHeight;
//-1 because when scroll reaches the bottom of the document windowScrollTop - documentHeightMinusWindowHeight equals from zero to 1
if ((windowScrollTop > documentMinusWindowHeight - 1) && !noMoreData && !inProgress) {
inProgress = true;
$("#loadingDiv").show();
$.post("@Url.Action("InfiniteScroll", "Products")", { "productId": '@Model.ProductViewModel.Id', "pageNumber": pageNumber },
function (data) {
pageNumber++;
noMoreData = data.NoMoreData;
$("#ProductDealsPartialDiv").append(data.HtmlString);
$("#loadingDiv").hide();
inProgress = false;
});
}
}
$(document).ready(function () {
//var pageNumber = 1;
//var noMoreData = false;
//var inProgress = false;
ScrollFunction(pageNumber, noMoreData, inProgress);
$(window).scroll(function () {
ScrollFunction(pageNumber, noMoreData, inProgress);
});
});
</script>
But this code snippet works absolutely correct.
<script type="text/javascript">
$(document).ready(function () {
var pageNumber = 1; //Infinite Scroll starts from second block
var noMoreData = false;
var inProgress = false;
$(window).scroll(function () {
var documentHeight = $(document).height();
var windowHeight = $(window).height();
var windowScrollTop = $(window).scrollTop();
var documentMinusWindowHeight = documentHeight - windowHeight;
//-1 because when scroll reaches the bottom of the document windowScrollTop - documentHeightMinusWindowHeight equals from zero to 1
if ((windowScrollTop > documentMinusWindowHeight - 1) && !noMoreData && !inProgress) {
inProgress = true;
$("#loadingDiv").show();
$.post("@Url.Action("InfiniteScroll", "Products")", { "productId": '@Model.ProductViewModel.Id', "pageNumber": pageNumber },
function (data) {
pageNumber++;
noMoreData = data.NoMoreData;
$("#ProductDealsPartialDiv").append(data.HtmlString);
$("#loadingDiv").hide();
inProgress = false;
});
}
});
});
</script>