2017-02-28 284 views
0

我希望有人能够找到我的问题。我的脚本只在向下滚动时触发...我需要该功能。它没有做的是在页面加载时也是我最初需要的。所以它首先加载内容,然后我可以向下滚动页面以获得更多新内容。Ajax在页面加载时触发

我试图把ajax放在一个函数里面,而函数的名字在外面,它仍然没有触发。

$(document).ready(function() { 
    $(window).scroll(function() { 
     if ($(window).scrollTop() >= $(document).height() - $(window).height() - 100) { 
      flag = true; 
      first = $('#first').val(); 
      limit = $('#limit').val(); 
      no_data = true; 
      if (flag && no_data) { 
       flag = false; 
       $('#loadmoreajaxloader').show(); 
       $.ajax({ 
        url: "commentedoncontent.php", 
        dataType: "json", 
        method: "POST", 
        cache: false, 
        data: { 
         start: first, 
         limit: limit 
        }, 
        success: function(data) { 
         flag = true; 
         $('#loadmoreajaxloader').hide(); 
         if (data.count > 0) { 
          first = parseInt($('#first').val()); 
          limit = parseInt($('#limit').val()); 
          $('#first').val(first + limit); 
          $.each(data.posts, function(i, response) { 
           //post content here 
          }); 
         } else { 
          alert('No more data to show'); 
          no_data = false; 
         } 
        }, 
        error: function(xhr, ajaxOptions, thrownError) { 
         alert(xhr.status); 
         flag = true; 
         no_data = false; 
        } 
       }); 
      } 
     } 
    }); 
}); 
+2

'ready'里面的部分只是附加一个滚动监听器。你想要加载哪个部分? – Carcigenicate

+0

我想在页面加载时触发ajax(页面刷新时),以便我可以看到新的内容。但是我也希望滚动功能在获取新内容方面发挥作用。它不会抛出任何错误,因为它不会触发页面加载。只有当我滚动。 – Gateway

+0

虽然你从来没有告诉它在页面加载时触发它。你在页面加载时所做的只是附加一个滚动监听器,它在滚动之前什么也不做。请参阅下面的答案。 – Carcigenicate

回答

1

你需要把它单独作为一个功能,这样你就可以调用它的滚动,并在页面加载:

$(document).ready(function() { 
    myFunction(); 
    $(window).scroll(function() { 
     if ($(window).scrollTop() >= $(document).height() - $(window).height() - 100) { 
      myFunction(); 
     } 
    }); 
}); 
function myFunction(){ 
    /* all the logic goes here */ 
} 
+0

非常感谢。这正是我需要的。感谢@LordNeo的帮助 – Gateway

1

你可以把你的代码中的函数,然后执行它在页面加载并在滚动。

$(document).ready(function() { 
    doStuff(); //choose the name that you want 

    $(window).scroll(function() { 
    doStuff(); 
    }); 
}); 

function doStuff(){  
     if ($(window).scrollTop() >= $(document).height() - $(window).height() - 100) { 
      flag = true; 
      first = $('#first').val(); 
      limit = $('#limit').val(); 
      no_data = true; 
      if (flag && no_data) { 
       flag = false; 
       $('#loadmoreajaxloader').show(); 
       $.ajax({ 
        url: "commentedoncontent.php", 
        dataType: "json", 
        method: "POST", 
        cache: false, 
        data: { 
         start: first, 
         limit: limit 
        }, 
        success: function(data) { 
         flag = true; 
         $('#loadmoreajaxloader').hide(); 
         if (data.count > 0) { 
          first = parseInt($('#first').val()); 
          limit = parseInt($('#limit').val()); 
          $('#first').val(first + limit); 
          $.each(data.posts, function(i, response) { 
           //post content here 
          }); 
         } else { 
          alert('No more data to show'); 
          no_data = false; 
         } 
        }, 
        error: function(xhr, ajaxOptions, thrownError) { 
         alert(xhr.status); 
         flag = true; 
         no_data = false; 
        } 
       }); 
      } 
     } 

}