1.0.3 • Published 1 year ago

array-item-ttl v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

array-item-ttl

Auto remove item from array with specific time.

Initialize

import arrayItemTTL from 'array-item-ttl'
const arr = new arrayItemTTL()

Example

import arrayItemTTL from 'array-item-ttl'
const arr = new arrayItemTTL()

arr.push('abc', 3000) // push an item with 3000ms time to life
arr.push('def', 2000)

setInterval(() => {
    console.log(arr.list)
}, 1000)

Output:

[ 'abc', 'def' ]
[ 'abc' ]
[]

Advanced Example

Setting a default time to life

import arrayItemTTL from 'array-item-ttl'
const arr = new arrayItemTTL(3000) // default time to life

arr.push('abc') // push an item with 3000ms ttl
arr.push('abc', 5000) // push an item with 5000ms ttl

If pushing the same item multiple times, ttl will be recount and updated by the last one.

arr.push('abc', 5000) // initially set ttl to 5000ms
arr.push('abc', 3000) // change ttl to 3000ms
arr.push('abc', 2000) // change ttl to 2000ms

setInterval(() => {
    console.log(arr.list)
}, 1000)

Output:

[ 'abc' ]
[ 'abc' ]
[]