首次调用后始终调用的Ajax回调函数

时间:2013-05-13 06:10:02

标签: c# asp.net jquery callback webmethod

我想使用ajax回调函数进行自动加载,但GetData函数首先使用相同的参数继续调用。这是我的javascript代码:

        var currentPage = 0;
        var isFinished = false;
        var lastScrollTop = 0;
     $(window).data('ajaxready', true).scroll(function (e) {
            if ($(window).data('ajaxready') == false) return;


            $(window).scroll(function (event) {
                var st = $(this).scrollTop();
                if (st > lastScrollTop) {
                    if (st > window.innerHeight) {
                        var amountValue = $("#amount").val();

                        var firstPrice = 0;
                        var lastPrice = 10000;

                        InfiniteScroll(firstPrice, lastPrice);
                    }

                } 

            });

        });



 function InfiniteScroll(firstPrice, lastPrice) {

            if (firstPrice < 0 || firstPrice == undefined) {
                firstPrice = 0;
            }
            if (lastPrice < 0 || lastPrice == undefined) {
                lastPrice = 10000;
            }

            if (isFinished) {
                return;
            }


            $('#divPostsLoader').html('<img src="images/loader.gif">');

            var rawPath = window.location.pathname.split('/');
            var categoryId = rawPath[rawPath.length - 1];

            $("#load").show();



            $.ajax({
                type: "POST",
                url: "http://localhost:60579/AjaxCallPage.aspx/GetData",
                data: "{categoryId:" + categoryId +
                            ",page:" + currentPage +
                            ",skip:'9',firstPrice:" + firstPrice + ",lastPrice:" + lastPrice + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    if (data.d.length < 3) {
                        isFinished = true;
                    }
                    var products = JSON.parse(data.d);
                    $.each(JSON.parse(data.d), function () {

                       // ... do smth..



                    });


                    currentPage += 1;

                    $("#load").hide();
                }
            });
        };

我不向下滚动,但在第一次调用后调用GetData函数。你有建议吗?

1 个答案:

答案 0 :(得分:0)

问题在于:

if (st > lastScrollTop) {
                if (st > window.innerHeight) {

检查出来,因为出于某种原因进入函数的这一部分而不滚动。如果我有时间,我会看看。