Jquery刷新正在创建一个不需要的无限循环

时间:2011-04-13 15:46:10

标签: jquery infinite-loop

我有一个设置为刷新的好友列表。每次刷新之后,开始将相同的名称添加到列表中,而不是仅刷新功能。任何想法都会很棒或者我错过了一些完整的东西?

$(function() {

$('<div />').html('<div id="buddies" class="buddies" style="vertical-align:bottom"><div class="imheader">&nbsp;Online Favorites<span class="fr"><a href="#" id="exitlist">X</a></span></div><div class="imheader2"><div class="imheaderspan"><a href="#">Options</a></div></div><ul class="imwindow"><div id="myonlinefavs"></div></ul></div><div id="chatbar" class="chatbar"><div id="ims"></div><div class="buddylist" id="buddylist"><center><a href="#" id="im-menu" class="im-menu">Favorites (<strong id="friendsonline">0</strong>)</a></center></div></div>').appendTo('body');

var imRoot = $("#im-menu");
var buddies = $("#buddies");
imRoot.click(function(){
buddies.toggle();
imRoot.blur();
return( false );
});
$("#exitlist").click(function(){
buddies.toggle();
imRoot.blur();
return( false );
});
$(document).click(function( event ){
if (buddies.is( ":visible" ) && !$( event.target ).closest( "#buddies" ).size()){
buddies.hide();
}
});
$(function FL(){
$.ajax({
    type: 'POST',
    url: 'config/receive_buddylist.php',
    cache: 'false',
    dataType: 'json',
    success: function(data){    
        onlineNumber = buddylistreceived = 0;
        data && $.each(data, function(i, e){
            if (i == "buddylist") {
                buddylistreceived = 1;
                totalFriendsNumber = onlineNumber = 0;
                $.each(e, function (l, f) {
                    if(f.id != null){
                        longname = f.n.length > 16 ? f.n.substr(0, 16) + "..." : f.n;
                        $('#myonlinefavs').append('<li>' + longname + '</li>');
                    }
                    if(f.s == '0') onlineNumber++;
                    totalFriendsNumber++;                           
                });
            }
        });
        R = onlineNumber;
        if (R > 0){
            $('#friendsonline').html(onlineNumber);
        } else {
            $('#myonlinefavs').html('No favorites online.');
            $('#friendsonline').html('0');
        }
    }
});
Z = setTimeout(function () {
    FL()
}, 1000)

});


});

1 个答案:

答案 0 :(得分:2)

在将列表项附加到$.each元素的myonlinefavs函数之前,您应该清除列表,如下所示:$('#myonlinefavs').html("")