I have a requirement to show real time update of the number of people who did some action. I implemented this functionality by making an ajax request to the server every 20 seconds. But this ajax request happens even if the tab is not in focus/no one is looking at the update. Is there a way to figure out if the tab is active?
I have the following code(simplified version) and it doesn't work.
timer = undefined
$(document).ready ->
timedCountUpdate()
window.top.onblur = ->
clearTimeout(timer)
window.top.onfocus = ->
timer = timedCountUpdate()
@timedCountUpdate = () ->
timer = setTimeout(updateCountIndicator, 20000)
@updateCountIndicator = () ->
$('.indicator').html = 100
timedCountUpdate()
I still see the call being made every 20s even if i am not in the tab that has the app loaded. I am testing in chrome.