2017-02-28 281 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; 
        } 
       }); 
      } 
     } 

}