소셜 네트워킹 사이트가 있고 기본 피드 페이지에 다음 jquery를 작성하여 사용자가 페이지 맨 아래에 가까이 갈 때 스크롤 할 때 게시물은 ajax로 검색되어 DOM에 추가됩니다. 브라우저는 각 Ajax 요청 후 느려지고 3 번째 또는 4 번째 크래시가 발생합니다.jQuery가 아약스에 의해 게시물을 얻으려면 브라우저가 충돌합니다.
feed_offset = posts_offset = post_comments_offset = 0;
loading_posts = loading_post_comments = loading_feed = false;
posts_end = post_comments_end = feed_end = false;
/* Automatically get new posts as the user scrolls down */
$(window).scroll(function() {
if ($('body').height() <= ($(window).height() + $(window).scrollTop() + 100)) {
// get more posts
if($('#all').is(':visible')) { // more of everything for all tab
if(loading_feed==false && feed_end==false) {
loading_feed = true;
$('#all .span12.columns .loading').show();
$.post('/feed/ajax_get_more_feed/'+feed_offset, function(data) {
$('#all .span12.columns .loading').hide().before(data);
feed_offset+= 10;
loading_feed = false;
if(data=='') {
feed_end = true;
}
});
}
} else if($('#posts').is(':visible')) { // more posts for posts tab
if(loading_posts==false && posts_end==false) {
loading_posts = true;
$('#posts .span12.columns .loading').show();
$.post('/feed/ajax_get_more_feed_posts/'+posts_offset, function(data) {
$('#posts .span12.columns .loading').hide().before(data);
posts_offset+= 10;
loading_posts = false;
if(data=='') {
posts_end = true;
}
});
}
} else { // more post comments for post comments tab
if(loading_post_comments==false && post_comments_end==false) {
loading_post_comments = true;
$('#post_comments .span12.columns .loading').show();
$.post('/feed/ajax_get_more_feed_post_comments/'+post_comments_offset, function(data) {
$('#post_comments .span12.columns .loading').hide().before(data);
post_comments_offset+= 10;
loading_post_comments = false;
if(data=='') {
post_comments_end = true;
}
});
}
}
}
});
나는이 코드를 추가 할 때까지 모든 것이 잘 실행되고이 모든 브라우저에서이 작업을하고있어 추가해야합니다 : 여기 내 코드입니다.
문제점을 발견했습니다. 그것은 ID보다는 클래스를 사용했기 때문입니다. 그래서 나는 다음과 같은 있었다 여기서
$('#all .span12.columns .loading').hide().before(data);
내가로드의 ID에로드의 클래스와 항목을 변경하고이 대신 수행했습니다
$('#all .span12.columns #loading').hide().before(data);
I 클래스이었다 사용 같은데요 jquery는 전체 DOM을 검색해야하지만 ID가있는 경우 ID는 1 개이므로 아이템을 찾을 때 검색을 중지 할 수 있습니다.
매번 더 많은 데이터를로드 할 때 브라우저의 메모리 공간은 어떻게됩니까? –
그것은 매번 올라갑니다! 파이어 폭스 페이지로드시 약 250,000k의 메모리를 사용하고 있었는데, 이것은 세 번째 요청에 의해 각각의 후속 아약스 요청과 함께 올라갔습니다.이 요청은 약 500,000k에서 최대치를 기록한 다음 추락했습니다. – geoffs3310
당신은 메모리를 차지하는 페이지에 점점 더 많은 데이터를 추가하고 있습니다. 당신은 Javascript에서 다른 메모리를 누출했을 수도 있습니다. 결국 웹 브라우저에 문제가 생길 수 있습니다. 그래서 우리는 무한 스크롤의 주요 문제를 배웁니다. –