最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Delete old divs if there are more than 20 | jQuery - Stack Overflow

programmeradmin6浏览0评论

I need some help with my jQuery script. I have a page that refreshes every 10 seconds and new divs from a feed are getting appended at.

My script counts the divs and removes the last div when there are more than 20 divs. This works fine if the feed just appends 1 div at a time. But the feed can also append multiply divs at the same time. When this happens the count can exceed the max of 20 divs. The problem with this is that my script just deletes 1 div and not all the divs that exceed the 20 count.

This is my code:

var $auto_refresh = setInterval(function () {
    var $articleCount = $('div').length; 

    if ($articleCount > 20) {
        $('div:last-child').remove();
    }

    $autoUpdate();
}, 10000); // refresh every 10000 milliseconds for new items

I need to remove all extra divs so there are always 20 divs. I hope someone can help me out with this.

I need some help with my jQuery script. I have a page that refreshes every 10 seconds and new divs from a feed are getting appended at.

My script counts the divs and removes the last div when there are more than 20 divs. This works fine if the feed just appends 1 div at a time. But the feed can also append multiply divs at the same time. When this happens the count can exceed the max of 20 divs. The problem with this is that my script just deletes 1 div and not all the divs that exceed the 20 count.

This is my code:

var $auto_refresh = setInterval(function () {
    var $articleCount = $('div').length; 

    if ($articleCount > 20) {
        $('div:last-child').remove();
    }

    $autoUpdate();
}, 10000); // refresh every 10000 milliseconds for new items

I need to remove all extra divs so there are always 20 divs. I hope someone can help me out with this.

Share Improve this question asked Feb 8, 2012 at 15:12 timGtimG 1271 silver badge6 bronze badges 0
Add a ment  | 

9 Answers 9

Reset to default 7

Use jQuery.slice to get everything past number 20, and bin them - dead easy :)

var $auto_refresh = setInterval(function () {
    $('div').slice(20).remove();
    $autoUpdate();
}, 10000); // refresh every 10000 milliseconds for new items

http://api.jquery./slice/

var $auto_refresh = setInterval(function () {
    var $articleCount = $('div').length; 

    while ($articleCount > 20) {
        $('div:last-child').remove();
        $articleCount = $('div').length;
    }

    $autoUpdate();
}, 10000);

Notice the change of if to while. This keeps deleting the last one until there are 20.

You could use .slice(x) to remove all elements from index x and on: http://jsfiddle/PLKAm/.

$("div").slice(20).remove();

If there are <= 20 items then .slice(20) returns an empty set, so the code is a no-op automatically.

Using the greater than selector:

var $auto_refresh = setInterval(function () {

    $('div:gt(20)').remove();

    $autoUpdate();
}, 10000); // refresh every 10000 milliseconds for new items
var $auto_refresh = setInterval(function () {

    while ($('div').length > 20) {
        $('div:last-child').remove();
    }

    $autoUpdate();
}, 10000); // refresh every 10000 milliseconds for new items
while ($articleCount > 20) {            
        $('div:last-child').remove();
        $articleCount = $('div').length;
    }

You could use the :gt() selector to find the elements in one fell swoop.

var $auto_refresh = setInterval(function () {
    var nToDelete = $('div').length - 20; // Calculate how many there are to delete...    
    if(nToDelete > 0) $('div:gt(" + nToDelete + ")').remove(); // and delete them.
    $autoUpdate();
}, 10000);
var remove21 = function() {
    if ($('div').length > 20) {
        $('div:nth-child(21)').remove();
        remove21();
    }
}

var $auto_refresh = setInterval(function () {

    remove21();

    $autoUpdate();
}, 10000); // refresh every 10000 milliseconds for new items

Edited for simplicity:

$('div').each(function(count){
    if(count >= 20){
        $(this).remove();
    }
});
发布评论

评论列表(0)

  1. 暂无评论