Created
June 15, 2022 19:00
-
-
Save metalaureate/a81324474b827c810113b3aca2ee01dc to your computer and use it in GitHub Desktop.
Generate a heartbeat event every 10 seconds when page tab is active
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
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