weixin_33725239 2015-10-09 06:11 采纳率: 0%
浏览 62

滚动发送ajax请求

I want to send ajax request when my page is first time scrolled to 20% of the actual height of the page .I have tried this but this is not working .

$(window).scroll(function(){
    if ($(window).scrollTop() >= ($(document).height() - $(window).height())*0.2){
        sendajax();
    }
});

Please suggest me what i have done wrong.I have to send only first time when scrolled to 20%

  • 写回答

1条回答 默认 最新

  • weixin_33674976 2015-10-09 06:35
    关注

    Problem is you are calling sendajax() on $(window).scroll(function().

    So every time you scrolled page it will check your condition in that block. After 20% your condition if ($(window).scrollTop() >= ($(document).height() - $(window).height())*0.2) always gone be true so it will call sendajax() every time you scroll page (after 20%).

    probable solution is you may set flag inside condition block like

    var isAjaxCalled= false; 
    $(window).scroll(function(){
    if (($(window).scrollTop() >= ($(document).height() - $(window).height())*0.2) && !isAjaxCalled){
    isAjaxCalled= true; 
        sendajax();
    }
    });
    

    This may helps you.

    thanks

    评论

报告相同问题?