views:

125

answers:

2

I currently have a page that contains an unordered list. That list is initially populated by querying my db, with each list item having a time stamp and text. Using javascript and AJAX, this page updates the unordered list dynamically every 10 seconds, if new data has come in.

What is the easiest and most efficient way to remove list items from this unordered list if the items are older than 24 hours?

My inclination is to do the following:

  1. In the js file, load all list items in the unordered list into an array
  2. During the js call to get data, if data comes back, add those items to the array as well
  3. Every time the getData() function is called, also call a removeData() function that removes all items that are older than 24 hours.

Also, I have had issues trying to figure out the correct way to add list items to a javascript array. Here is the code that I have been trying for adding all list items to an array:

  var list = new Array();
  $(".listname").each(function (i){
    list.push($(this));
  });

to remove list items from the array, I expect to use:

list.pop();
A: 

Your approach to expring elements when you run getData seems sound. You should append to the end of the array with push but pop just pulls elements from the end of the array, which is probably not the right order. Javascript arrays act like stacks rather than queues. You might want to add remove to the javascript array prototype as is suggested here

stimms
+1  A: 

Why not sort the array by date (oldest first), then use list.shift()? That will remove the first item in the array, as described here.

fudgey