ifvisible.js
Crossbrowser & lightweight way to check if user is looking at the page or interacting with it.
Check out the Demo or read below for code example or Check Annotated Source
Installation
From npm
npm install ifvisible.js --save
From Bower
bower install ifvisible.js
For Meteor
mrt add ifvisible
meteor package is provided by @frozeman via Atmosphere
Examples
if( ifvisible.now() ){
openPopUp();
}
if( !ifvisible.now('hidden') ){
openPopUp();
}
Handle tab switch or browser minimize states
ifvisible.on("blur", function(){
animations.pause();
});
ifvisible.on("focus", function(){
animations.resume();
});
ifvisible.js can handle activity states too, such as being IDLE or ACTIVE on the page
ifvisible.on("idle", function(){
stream.pause();
});
ifvisible.on("wakeup", function(){
stream.resume();
});
Default idle duration is 60 seconds but you can change it with setIdleDuration
method
ifvisible.setIdleDuration(120);
You can manually trigger status events by calling them directly or you can set events with their names by giving first argument as a callback
ifvisible.idle();
ifvisible.idle(function(){
});
ifvisible.blur();
ifvisible.idle();
ifvisible.focus();
ifvisible.wakeup();
You can set your smart intervals with ifvisible.js, if user is IDLE or not seeing the page the interval will automatically stop itself
ifvisible.onEvery(0.5, function(){
animateLogo();
});
License
It's MIT, Go crazy.