Skip to content

Instantly share code, notes, and snippets.

@metalaureate
Created June 15, 2022 19:00
Show Gist options
  • Save metalaureate/a81324474b827c810113b3aca2ee01dc to your computer and use it in GitHub Desktop.
Save metalaureate/a81324474b827c810113b3aca2ee01dc to your computer and use it in GitHub Desktop.
Generate a heartbeat event every 10 seconds when page tab is active
const CURRENT_PAGE='homepage' // whatever current page and/or pod id is
const poller=(page) => {
console.log(page)
return setInterval(() => {
let faux_segment_event={
'event': 'pods_heartbeat',
'page': page,
'timestamp': new Date().toUTCString(),
}
console.log('❤️🩺',faux_segment_event)
},10000)
}
const heartbeat={'poller': poller(CURRENT_PAGE)} // initializes heartbeat on page load
document.addEventListener('visibilitychange', function () {
if (document.hidden) {
console.log('💔 FAREWELL');
clearInterval(heartbeat.poller)
} else {
console.log('😍 WELCOME BACK');
heartbeat.poller=poller(CURRENT_PAGE)
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment