Open dasilvacontin opened 9 years ago
I solved it by calling ifvisible.now()
once before my code.
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?
I can't call init
manually? What would be the "way to go" as an user?
now
method automatically calls init
. This shouldn't be a problem. What are your steps to reproduce?
Sorry for the late reply
I was only using ifvisible.now()
. Therefore, the listeners hadn't been set up yet, on the first query/check.
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.
Weird. Maybe the change wasn't on npm
?
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
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();
};
@csilcock Thanks for detail explanation, I'll look into this tonight.
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'.
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.
This problem is now fixed on the beta version. Please check my comment on #38
ping, it still does not work in "ifvisible.js": "~1.0.6",
ifVisible.now()
is true
on background loaded pages
+1 still not working
Reproduced in Chrome Version 41.0.2272.76 (64-bit), Yosemite 10.10.2 (14C109).