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

How to auto remove elements out of an array after X minute(s) in JavaScript? - Stack Overflow

programmeradmin0浏览0评论

I don't know if this is a silly idea. But let's say I have an array, I keep pushing new elements to it. And I want to set up logic so that any element that exists for 5 minutes will be automatically removed from that array.

Any idea for the solution?

Note that I don't want to keep the timestamp for any element, I want them to be removed automatically.

I don't know if this is a silly idea. But let's say I have an array, I keep pushing new elements to it. And I want to set up logic so that any element that exists for 5 minutes will be automatically removed from that array.

Any idea for the solution?

Note that I don't want to keep the timestamp for any element, I want them to be removed automatically.

Share Improve this question asked Apr 27, 2018 at 16:05 ken.ngken.ng 2411 gold badge4 silver badges16 bronze badges 5
  • Sounds like you're asking for trouble, array elments are accessed by it's index so if you have const myArray = ["Hello","World"] and myArray[0] would be "Hello". But then 5 minutes later myArray[0] would be "World" or undefined if "World" is also removed. – HMR Commented Apr 27, 2018 at 16:10
  • There is no built in method for this, you would need to store a timestamp to know when the element had reached 5 minutes old. So in it's current state this question is un-answerable. – DBS Commented Apr 27, 2018 at 16:10
  • Are you willing to use redis for this feature? – Rash Commented Apr 27, 2018 at 16:10
  • 3 Stackoverflow is not intended to be a discussion board to brainstorm ideas. You are expected to have made your design choices already but if you have specific problems with a specific subset of the implementation, then please post your code in a Minimal, Complete, and Verifiable example and we can help you fix any issues. – Nope Commented Apr 27, 2018 at 16:10
  • Alright thanks guys, guess I'll just stick to the old way~ – ken.ng Commented Apr 27, 2018 at 16:14
Add a ment  | 

3 Answers 3

Reset to default 3

You can create your own array class and every time you push element you setTimeout its removal. Here we push a new element every second and they are automatically removed (without shifting the remainig elements) in 5 seconds.

class MyArray extends Array {
  push() {
    const i1 = this.length;
    const i2 = super.push(...arguments);
    setTimeout(() => {
      for (let i=i1; i<i2; i++) delete this[i];
    },5000);
    return i2;
  }
}

let a = new MyArray();
let i = 0;

let t1 = setInterval(() => { a.push(i++); console.log(a); }, 1000);
setTimeout(() => {
  clearInterval(t1);
}, 60000)

You can create setInterval that will clear items and when adding item to array add time when it was added.

var array = [];

function push(function(value) {
   array.push({
      value: value,
      time: Date.now()
   });
}

setInterval(function() {
  var time = Date.now();
  array = array.filter(function(item) {
     return time < item.time + (5000 * 60);
  }); 
}, 500);

You can model your element to be an object, which besides its value has a timestamp attribute.

Them you can call a method using setInterval, lets say with 100 milisec, to check whether or not remove the object from array, based in the current timestamp.

发布评论

评论列表(0)

  1. 暂无评论