I implemented script where every x minutes API will be called to show few pieces of data on the small dashboard.
I want to improve it a bit in a way to NOT query the API if user minimized the browser, tab not active, mouse not moving etc. In short if there is no activity detected in the particular tab dont send the request.
Honestly not sure. Any advice would be great. I mean if they not engaged with the page after X many minutes stop querying. if they have page on another page and reading yes then get the data but I do want to stop getting data if there is no activity. hope that make sense.
But if i set your site on another monitor and just have it there, the document sees no activity. Remember that Javascript in a webpage cant penetrate the barrier and observe things outside of the document. I could be clicking 1000 times a minute on the other window while your site is over there, but your site sees no activity.
Not sure if this is close, but maybe an idea to work from.
window.addEventListener('DOMContentLoaded', () => {
function afterInterval(callback, delay = 5000) {
// set the initial timer
let timer = setTimeout(callback, delay)
const handler = () => {
// cancel the previous timer
clearTimeout(timer)
// set a new one going
timer = setTimeout(callback, delay)
}
// mousemove or click will cancel timer and trigger a new one
window.addEventListener('mousemove', handler)
window.addEventListener('click', handler)
}
afterInterval(() => alert('Do you want to continue?'))
})