Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dasilvacontin opened this issue Apr 3, 2015 · 16 comments
Open

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

dasilvacontin opened this issue Apr 3, 2015 · 16 comments
Assignees
Labels
Milestone

Comments

@dasilvacontin
Copy link

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

@dasilvacontin
Copy link
Author

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

@dasilvacontin
Copy link
Author

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
Copy link
Author

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

@serkanyersen serkanyersen added this to the v1.0.2 milestone May 1, 2015
@serkanyersen serkanyersen self-assigned this May 1, 2015
@serkanyersen serkanyersen modified the milestones: v1.0.4, v1.0.2 May 24, 2015
@serkanyersen
Copy link
Owner

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

Sorry for the late reply

@dasilvacontin
Copy link
Author

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

@serkanyersen
Copy link
Owner

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
Copy link
Author

Weird. Maybe the change wasn't on npm?

@serkanyersen
Copy link
Owner

Checked the history, init was in now even before now has any other code.

init(); // Auto init on first call

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

@csilcock
Copy link

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
Copy link
Owner

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

@hartmut-co-uk
Copy link

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
Copy link

SirNovi commented Oct 14, 2016

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
Copy link
Owner

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

@golyshevd
Copy link

golyshevd commented Feb 14, 2017

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

@golyshevd
Copy link

ifVisible.now() is true on background loaded pages

@tcastelli
Copy link

+1 still not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants