我在Home.xhtml上使用延迟滚动。问题是每当我向下滚动到pape的底部时,Javascript会不停地调用getMoreStatusList.xhtml页面上的getMoreStatusList
函数,直到编译器抛出索引超出范围的异常。我该如何解决这个问题?
1)Home.xhtml
<h:head>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
/* <![CDATA[ */
$(document).ready(function() {
function lastAddedLiveFunc() {
$('div#lastPostsLoader').html('<img src="bigLoader.gif">');
$.get("getMoreStatusList.xhtml", function(data) {
if (data != "") {
//console.log('add data..');
$(".items").append(data);
}
$('div#lastPostsLoader').empty();
});
};
//lastAddedLiveFunc();
$(window).scroll(function(){
var wintop = $(window).scrollTop(), docheight = $(document).height(), winheight = $(window).height();
var scrolltrigger = 0.95;
if ((wintop/(docheight-winheight)) > scrolltrigger) {
//console.log('scroll bottom');
lastAddedLiveFunc();
}
});
});
/* ]]> */
</script>
</h:head>
<h:body class="thrColElsHdr">
<!-- -->
<div class="items">
<div id="scroll_items">
<c:forEach var="p" items="#{statusBean.statusList}">
<h:form>
<div class="status">
<c:if test="${p.statusmsg!=null}">
<!-- for text status msg check-->
<h:commandLink
action="#{friendBean.gotoFriendProfile(p.email)}"
styleClass="link">
<img src="../images/profilePicture/thumb/#{p.picture}"
style="height: 39px; width: 39px;" /> <h:outputText
value="#{p.statusBy}:" />
</h:commandLink>
<br />
</c:if>
</c:forEach>
</div>
</div>
</h:body>
</html>
2)Status Bean(包含getMoreStatusList函数)
public class StatusBean {
public List<Status> getStatusList() {
FacesContext context = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) context.getExternalContext()
.getSession(true);
User user = (User) session.getAttribute("userdet");
Query query = em.createQuery("SELECT s FROM Status s WHERE s.email='"
+ user.getEmail() + "' ORDER BY s.timeMillis desc",
Status.class);
List<Status> results = query.getResultList();
Collections.sort(results);
results = results.subList(0, 5);
session.setAttribute("statusindex", 5);
return results;
}
public List<Status> getMoreStatusList() {
System.out.println("Inside getMoreStatusList");
FacesContext context = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) context.getExternalContext()
.getSession(false);
User user = (User) session.getAttribute("userdet");
Query query = em.createQuery("SELECT s FROM Status s WHERE s.email='"
+ user.getEmail() + "' ORDER BY s.timeMillis desc",
Status.class);
List<Status> results = query.getResultList();
Collections.sort(results);
int index = (int) session.getAttribute("statusindex");
System.out.println(index);
results = results.subList(index,index+5);
session.setAttribute("statusindex", index + 5);
return results;
}
}
3)getMoreStatusList.xhtml
<h:head></h:head>
<h:body>
<c:forEach var="p" items="#{statusBean.moreStatusList}">
<h:form>
<div class="status">
<c:if test="${p.statusmsg!=null}">
<!-- for text status msg check-->
<h:commandLink action="#{friendBean.gotoFriendProfile(p.email)}"
styleClass="link">
<img src="../images/profilePicture/thumb/#{p.picture}"
style="height: 39px; width: 39px;" /> <h:outputText
value="#{p.statusBy}:" />
</h:commandLink>
<h:outputText value="#{p.statusmsg}" styleClass="textstyle1" />
<h:outputText value="#{p.timeMillis}"
style="font-size:xx-small;float:right;color:#bbbbbb;font-style: italic;">
<f:converter converterId="timeConverter" />
</div>
</c:forEach>
<br />
<div class="comment">
<p:inputText value="#{statusBean.comment.comment}"
styleClass="box" />
<p:commandLink value="Views"
action="#{statusBean.update(p.statusId)}" ajax="false"
styleClass="link" />
</div>
<br />
</c:if>
</c:forEach>
</h:body>
</html>
答案 0 :(得分:1)
在我看来,当您向下滚动时滚动被多次调用,因为当您向下滚动时,对于像素范围,表达式wintop/(docheight-winheight)) > scrolltrigger
将为真。您可以通过在lastAddedLiveFunc()上保持警报来验证它。如果情况确实如此,
保留一个像'gettingNextScrollData'这样的变量来验证ajax调用是否已在进行中。
<script type="text/javascript">
/* <![CDATA[ */
var gettingNextScrollData = false;
$(document).ready(function() {
function lastAddedLiveFunc() {
$('div#lastPostsLoader').html('<img src="bigLoader.gif">');
gettingNextScrollData = true;
$.get("getMoreStatusList.xhtml", function(data) {
if (data != "") {
//console.log('add data..');
$(".items").append(data);
}
$('div#lastPostsLoader').empty();
gettingNextScrollData = false;
});
};
//lastAddedLiveFunc();
$(window).scroll(function(){
if(gettingNextScrollData == false) {
var wintop = $(window).scrollTop(), docheight = $(document).height(), winheight = $(window).height();
var scrolltrigger = 0.95;
if ((wintop/(docheight-winheight)) > scrolltrigger) {
lastAddedLiveFunc();
}
}
});
});
/* ]]> */
</script>