serkanyersen / ifvisible.js

Crossbrowser & lightweight way to check if user is looking at the page or interacting with it.
http://serkanyersen.github.com/ifvisible.js/
MIT License
1.95k stars 185 forks source link

wrong value for ifvisible.now() the first time it's called #21

Open dasilvacontin opened 9 years ago

dasilvacontin commented 9 years ago

Reproduced in Chrome Version 41.0.2272.76 (64-bit), Yosemite 10.10.2 (14C109).

dasilvacontin commented 9 years ago

I solved it by calling ifvisible.now() once before my code.

dasilvacontin commented 9 years ago

I see. If you are only using ifvisible.now() in your code, the listeners haven't been initialised yet. This needs to be documented, or did I miss the mention of this case?

dasilvacontin commented 9 years ago

I can't call init manually? What would be the "way to go" as an user?

serkanyersen commented 9 years ago

now method automatically calls init. This shouldn't be a problem. What are your steps to reproduce?

Sorry for the late reply

dasilvacontin commented 9 years ago

I was only using ifvisible.now(). Therefore, the listeners hadn't been set up yet, on the first query/check.

serkanyersen commented 9 years ago

This must be some other problem, as you can see here https://github.com/serkanyersen/ifvisible.js/blob/master/src/ifvisible.coffee#L433 now calls init by itself.

dasilvacontin commented 9 years ago

Weird. Maybe the change wasn't on npm?

serkanyersen commented 9 years ago

Checked the history, init was in now even before now has any other code. https://github.com/serkanyersen/ifvisible.js/blob/35a5b95540f7fb2a6aac27eec9098c23568df239/ifvisible.js#L155

Must be some other bug. I'll look out for it

csilcock commented 9 years ago

I ran into this as well while trying to rely on ifvisible.Now( 'hidden' ) to prove visibility without the idle tracking. It's because init() (which is called by now() ) calls trackIdleStatus() at return, which in turn calls wakeUp(), resetting the status to 'active'.

I solved this by returning this from init():

return trackIdleStatus(), initVisibility();

where the definition of initVisibility() is as follows:

initVisibility = function() {
    if (doc[hidden]) {
        return ifvisible.blur();
    } else {
        return ifvisible.focus();
    }
};

Note: this fix does not work with IE9 or earlier (I don't need the legacy support). I'm not sure there is any way to make it work with IE9, as initial state depends on the html5 visibility API.

Hope this helps.

My full compiled init, fyi:

init = function() {
  var blur;
  if (initialized) {
    return true;
  }
  if (hidden === false) {
    blur = "blur";
    if (ie < 9) {
      blur = "focusout";
    }
    addEvent(window, blur, function() {
      return ifvisible.blur();
    });
    addEvent(window, "focus", function() {
      return ifvisible.focus();
    });
  } else {        
    addEvent(doc, visibilityChange, initVisibility, false);
  }
  initialized = true;

  //The initVisibility call will not work in IE 9 or less, but it doesn't error out.
  //It's called here as trackIdleStatus() calls wakeUp(), resetting the visibility status to true
  return trackIdleStatus(), initVisibility();
};
serkanyersen commented 9 years ago

@csilcock Thanks for detail explanation, I'll look into this tonight.

hartmut-co-uk commented 8 years ago

Hi, I can confirm ifvisible.now() always returns 'true' when used plainly without listeners. I would like to check if a page was opened 'in a new tab' = 'hidden in background'.

SirNovi commented 7 years ago

I wanted to use this plugin for the sole purpose of delaying an animation until a user actually sees the page for the first time. But as mentioned before, the ifvisible.now() returns true if the page is opened in a new tab (and not yet visible) as well.

serkanyersen commented 7 years ago

This problem is now fixed on the beta version. Please check my comment on #38

golyshevd commented 7 years ago

ping, it still does not work in "ifvisible.js": "~1.0.6",

golyshevd commented 7 years ago

ifVisible.now() is true on background loaded pages

tcastelli commented 7 years ago

+1 still not working