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

Make inactivity timeout configurable #1409

Merged
merged 8 commits into from
Sep 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ vjs.options = {
// Add playback rate selection by adding rates
// 'playbackRates': [0.5, 1, 1.5, 2],

// default inactivity timeout
'inactivityTimeout': 2000,

// Included control sets
'children': {
'mediaLoader': {},
Expand Down
23 changes: 13 additions & 10 deletions src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1615,16 +1615,19 @@ vjs.Player.prototype.listenForUserActivity = function(){
// Clear any existing inactivity timeout to start the timer over
clearTimeout(inactivityTimeout);

// In X seconds, if no more activity has occurred the user will be
// considered inactive
inactivityTimeout = setTimeout(vjs.bind(this, function() {
// Protect against the case where the inactivityTimeout can trigger just
// before the next user activity is picked up by the activityCheck loop
// causing a flicker
if (!this.userActivity_) {
this.userActive(false);
}
}), 2000);
var timeout = this.options()['inactivityTimeout'];
if (timeout > 0) {
// In <timeout> milliseconds, if no more activity has occurred the
// user will be considered inactive
inactivityTimeout = setTimeout(vjs.bind(this, function () {
// Protect against the case where the inactivityTimeout can trigger just
// before the next user activity is picked up by the activityCheck loop
// causing a flicker
if (!this.userActivity_) {
this.userActive(false);
}
}), timeout);
}
}
}), 250);

Expand Down
51 changes: 51 additions & 0 deletions test/unit/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,54 @@ test('should restore attributes from the original video tag when creating a new
equal(el.getAttribute('controls'), '', 'controls attribute was set properly');
equal(el.getAttribute('webkit-playsinline'), '', 'webkit-playsinline attribute was set properly');
});

test('should honor default inactivity timeout', function() {
var player, html5Mock;
var clock = sinon.useFakeTimers();

// default timeout is 2000ms
player = PlayerTest.makePlayer({});

equal(player.userActive(), true, 'User is active on creation');
clock.tick(1800);
equal(player.userActive(), true, 'User is still active');
clock.tick(500);
equal(player.userActive(), false, 'User is inactive after timeout expired');

clock.restore();
});

test('should honor configured inactivity timeout', function() {
var player, html5Mock;
var clock = sinon.useFakeTimers();

// default timeout is 2000ms, set to shorter 200ms
player = PlayerTest.makePlayer({
'inactivityTimeout': 200
});

equal(player.userActive(), true, 'User is active on creation');
clock.tick(150);
equal(player.userActive(), true, 'User is still active');
clock.tick(350);
// make sure user is now inactive after 500ms
equal(player.userActive(), false, 'User is inactive after timeout expired');

clock.restore();
});

test('should honor disabled inactivity timeout', function() {
var player, html5Mock;
var clock = sinon.useFakeTimers();

// default timeout is 2000ms, disable by setting to zero
player = PlayerTest.makePlayer({
'inactivityTimeout': 0
});

equal(player.userActive(), true, 'User is active on creation');
clock.tick(5000);
equal(player.userActive(), true, 'User is still active');

clock.restore();
});