Skip to content

ThisIsHermanCheng/array-item-ttl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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' ]
[]

About

Auto remove item from array with specific time

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published