Visibility.every
is an analogue of setInterval
, but only works if the site is in an open tab).
<video>
. If there is support, it will be more convenient for the user; if not, the site will work like regular sites and think that the user always sees the site. High-level functions in Visibility.js are specifically made so that the developer can not think about whether there is support for the API or not.
visible
- the user now sees the page.hidden
- the page is not visible to the user, since another tab is open in the browser, the browser window is minimized or the OS has completely blocked the screen. The truth is, in reality, Chrome only checks whether the current tab is open or not; minimizing the browser has no effect.prerender
- the browser loaded and rendered the page in advance, to show it to the user instantly. That is, now the user does not see the page, unnecessary calculations and multimedia should be removed, and in statistics this view is not yet considered. All technology is supported only in Google Chrome, although it already exists in the standard.preview
- the site is open in a small preview window. For example, in the mozayka of frequently opened sites in the new tab. The theoretical property of the standard, since it is not supported by any browser.document.hidden
property (don't forget about vendor prefixes, in Chrome it will be document.webkitHidden
) or the Visibility.hidden()
method in Visibility.js. If you need to check whether the site is visible - use this property, and not compare the name of the state with "hidden"
.
Visibility.every(interval, callback)
is an analogue of setInterval(callback, interval)
, but starts the callback
only if the user now sees the page.
Visibility.every(1000, function() { updateCountdownAnimation(); });
Visibility.every(visible, hidden, callback)
can take 2 time intervals - visible
will use when the page is visible to the user, and hidden
- when hidden.
var minute = 60 * 1000; Visibility.every(minute, 5 * minute, function () { checkForEmail(); });
Visibility.every('minute', '5 minutes', function () { checkNewMails(); });
Visibility.every
, use Visibility.stop(timerID)
( clearInterval
will not work):
var slideshow = Visibility.every(5 * 1000, function () { nextSlide(); }); $('.stopSlideshow').click(function () { Visibility.stop(slideshow); });
Visibility.every
will assume that the user always sees the site (that is, it becomes a complete analog of setInterval
, but nothing terrible happens).
Visibility.onVisible(callback)
method that executes the code only when the page becomes visible. If the page is already visible, the callback
called right away.
Visibility.onVisible(function () { setTimeout(function() { Notification.hide(); }, 10 * 1000); });
Visibility.onVisible(callback)
will trigger a callback
right away.
rel="prefetch"
rel="prefetch"
for links, which tells the browser that the user is likely to open the link later, so the browser loads its contents in advance. This is necessary, for example, to download the next chapter of the article or for the first result in search results.
rel="prerender"
rel="prerender"
- it not only loads, but also renders the page in advance to open it instantly ( video example from Google).
Visibility.afterPrerendering(callback)
, which will perform a callback
only when the page opens for real (that is, leaves the state of pre-rendering) or performs a callback
immediately, if the page was immediately opened normally. In the callback
you can enable auto-update via AJAX, add it to the <video>
page and count the user in the statistics.
Visibility.afterPrerendering(function () { Statistics.countVisitor(); });
Visibility.afterPrerendering(callback)
will trigger the callback
immediately.
Visibility.isSupported()
returns true
if the browser supports Page Visibility API. The browser does not support Page Visibility API, you can easily find out - it will have document.hidden
undefined
, not true
or false
(just do not forget about the vendor prefix, for example, document.webkitHidden
).
Visibility.state()
returns the state name ( "visible"
, "hidden"
or "prerenderer"
). This method simply looks at the document.visibilityState
property, taking into account the vendor prefix (for example, document.webkitVisibilityState
). A small example to fix:
if( Visibility.isSupported() ) { if ( 'hidden' == Visibility.state() ) { Statistics.userOpenPageInBackgroundTab(); } if ( 'prerender' == Visibility.state() ) { Statistics.pageIsPrerendering(); } })
Visibility.hidden()
(since the list of states can be replenished in the future). She just looks at the document.hidden
property. The following example includes autoplaying video only if the page is opened immediately in the active tab (and not in the new background):
$(document).load(function () { if ( !Visibility.hidden() ) { VideoPlayer.play(); } });
document
has a visibilitychange
event (in Chrome, webkitvisibilitychange
). In Visibility.js there is a shorter way - the method Visibility.change(callback)
itself hangs up the event handler and calls a callback
every time the page visibility changes. The first argument to the callback
is the event object, and the second is the name of the state. Example:
Visibility.change(function (e, state) { Statistics.trackChangeVisibility(state); });
visibilityjs
gems in the gemfile:
gem 'visibilityjs'
app/assets/javascripts/application.js.coffee
:
#= require visibility
public/javascripts/lib
and config/assets.yml
Visibility.js in the package settings in config/assets.yml
:
javascripts: application: - public/javascripts/lib/visibility.js
Source: https://habr.com/ru/post/125833/