开发者

$.get() function called three times when triggered, how to prevent it

I have got main html page called myitem.html, then I used ajax to display the itembox which is loaded from itembox.php file, And When I scroll down inside the itembox, the new content from the new.php file are appended in the end of the itembox, However the newcontent in the itembox are appended thress time which all the same, How can I only append one times,

here is part of the html code in itembox.php

<div id="itemscrollbox">
    <ul id = "scroll">
        <li>0 item</li>
        <li>0 item</li>
        <li>0 item</li>
        <li>0 item</li>
        <li>0 item</li>
    </ul>
</div>

here is part of the javascript code for scrolling update in the itembox.php file:

function scroll() {
    var scrolltop = $('#itemscrollbox').attr('scrollTop');
    var scrollheight = $('#itemscrollbox').attr('scrollHeight');
    var windowheight = $('#itemscrollbox').attr('clientHeight');
    var scrolloffset = 0;
    if (scrolltop >= (scrollheight - (windowheight + scrolloffset))) {
        var ID = $("#myscroll li:last").attr("id");
        $('#status').text('Loading more items...');
        $.get('picturedata.php', 'id=' + ID + '', function(newite开发者_如何学Gom) {
            $('#scroll').append(newitem);
            updateitemstatus();
        });
    }
    setTimeout('scroll();', 2500);
}

here is part of the code in new.php file:

  • first items
  • second items
  • the output should give me:

    first item
    second item
    

    but it gives me:

    first item
    second item
    first item
    second item
    first item
    second item
    

    Any one could tell me how to resolve this problem, thanks a lot!

    here is how the scroll function called:

         $(document).ready(function(){
    
        updateitemstatus();
    
    
       scroll();
           });
    

    i assume that because the main html page which is myitem.html load itembox inside it, and itembox append new content from new.php while scrolling down, so that the function $.get are called three times, but I couldn't figure out how to avoid that.


    You should $.clear() your result div before appending.

    $('#scroll').clear();
    
    $.get('picturedata.php', 'id='+ ID+'', function(newitem){
        $('#scroll').append(newitem);
        updateitemstatus();
    });
    
    0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新问答

    问答排行榜