This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a buffer | |
var buf = [] | |
buf.push({ timestamp: Date.now(), value: whatever }) | |
setInterval(function () { | |
purge(buf, 120 * 1000) | |
}, 1000) | |
function purge(buffer, timeToLive) { | |
var now = Date.now() | |
// while the first item in the buffer is too old | |
// remove the head of the buffer. | |
// terminates when the tail of the buffer is recent | |
// this works because buffer is sorted chronologically | |
while (buffer[0] && buffer[0].timestamp < now - timeToLive) { | |
buffer.shift() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment