Jquery new div fadIn effect, reloading div

I have div content, in this div I have multiple div comments, I reload the div content every 5 seconds, and I would like to fadeIn every new comment. To do this, each comment has an id which is an integer, so basically what I do: I store the id of the last comment in the "lastComment" variable, and when I reload the content of the "div" div, I use if ($ (". Comment "). Attr (id)> lastComment) {alert ('ok')}

But it doesn't work, first it reloads the div and instead of saying "ok" I need to wait another 5 seconds, here is my code:

var lastComment = $('.comment:last').attr('id');
setInterval(function()
{
    $('#content').load('fullscreen.php?image='+$('.imageBig').attr('id')+' #content');
    if($('.comment').attr('id') > lastComment){
        alert('ok');
    }
}, 5000);

      

please come up with?

+3


source to share


1 answer


I think there is your problem jQuery selector

, in the if condition you have to specify the last div.comment

one because the statement var lastComment = $('.comment:last').attr('id');

will assign the last element once after that setInterval

and the new last div comment will be changed so the warning won't work.



var lastComment = $('.comment:last').attr('id');
setInterval(function()
{
    $('#content').load('fullscreen.php?image='+$('.imageBig').attr('id'),function(){
        if($('.comment:last').attr('id') > lastComment){
            alert('ok');
        }
    });

}, 5000);

      

+1


source







All Articles