2014-02-28 55 views
0
var p = 1; 
var flag = 0; 
$(window).scroll(function(){ 
    if(flag == 0){ 
     if($(window).scrollTop() == $(document).height() - $(window).height()){ 
      $.get('/fetchMore.php?page=' + p, function(data){}); 
        p++ 
      } 
    } 
});       

我有一个页面,当向下滚动时,页面将使用$ .get来获取更多的数据。在我fetchMore.php

我已经设置了如果没有更多的数据,$标记== 1,我需要出去把JavaScript来更新变种的标志,所以滚动功能将无法再获取

我的问题是如何从fetMore.php页面更新var标志?

回答

1

您不能直接从ajax调用php来更改javascript变量。当没有更多数据可用时,您可能会传递一个空数据,并检查javascript中的数据是否为空并将标志分配给1

标志被分配为1时,它将不会再获取。

var p = 1; 
var flag = 0; 
$(window).scroll(function(){ 
    if(flag == 0){ 
     if($(window).scrollTop() == $(document).height() - $(window).height()){ 
      $.get('/fetchMore.php?page=' + p, function(data){ 
       if(data == {} || data == [] || data == "") { 
        flag = 1; 
       } 
      }); 
     } 
    } 
}); 
0

当你从它被JS收到你的PHP脚本提出申请,要求它这样echo东西:

echo $flag从你的PHP脚本,然后:

$.get('/fetchMore.php?page=' + p, function(data){ 
    if(data == 1) { 
     flag = 1 
    } 
} 

你可能要检查在您的PHP脚本中,如果数据可用或不可用。

if(!$data.length){ 
    // there is no more data 
    echo "1"; // or something more useful 
    exit(); // this stops further execution of the script 
} 
// if there is data the script will continue to run and you can echo or do anything else later.. 
+0

我会输出很多数据,我不想在第 – Ben

+0

页回显1 @BenWong检查更新的答案 –