-6

Is there a way to track if user is not active on page (he opened our website, but later he opened i.e. facebook and he forgot our page)? I want to give him blinking title in my website tab, to grab his attention back.

For example, regular title is "Book your room" and if he open something else in other tab I want to change title from "Book your room" to "You forgot to book your room" and vice versa on like 2 seconds loop.

Would be nice if it can be done in AngularJS. Thanks!

R. Richards
  • 24,603
  • 10
  • 64
  • 64
Milos Petrovic
  • 183
  • 1
  • 2
  • 9
  • Possible duplicate of https://stackoverflow.com/questions/1009260/how-can-i-detect-with-javascript-jquery-if-the-user-is-currently-active-on-the-p (That is an older question, but because your question does not give the impression that you made an actual effort to properly research this _at all_, it should do as a starting point.) – misorude Nov 02 '18 at 10:23
  • https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API would be the more modern approach. – misorude Nov 02 '18 at 10:24
  • @misorude I found that question, but it's not solution for me. It's not the best solution at all. Page Visibility API is a good one, it would do the job much better. Thanks! – Milos Petrovic Nov 02 '18 at 10:29
  • 1
    Fail #1: No mention of what you (supposedly) found yourself. Fail #2: No explanation _why_ you do not consider it a solution. Fail #3: Using the phrase “best solution” without specifying _any_ criteria what would make a solution “good” or “best”. – misorude Nov 02 '18 at 10:31

1 Answers1

0
<script>
var timer = null;
function setBlurTitle(num) {
    let n = 1;
    if (num) {
        n = num;
    };
    if (n % 2 == 0) {
        document.title = "come back here";
    } else {
        document.title = "";
    };
    if (timer) {
        clearTimeout(timer);
        timer = null;
    };
    timer = setTimeout(() => {
        n++;
        setBlurTitle(n);
    }, 500);
}
function clearTitle() {
    document.title = 'normal'
    if (timer) {
        clearTimeout(timer);
        timer = null;
    }

}
window.addEventListener('blur', () => { setBlurTitle() }, false);
window.addEventListener('focus', () => { clearTitle() }, false)