Skip to content

Commit

Permalink
feat: allow turning off timeout by setting it to Infinity (#204)
Browse files Browse the repository at this point in the history
Fixes #134
  • Loading branch information
gkatsev authored Apr 13, 2021
1 parent d8d9efc commit b73f160
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Maintenance Status: Stable
- [Custom Errors without a Type](#custom-errors-without-a-type)
- [`getAll()`](#getall)
- [`timeout()`](#timeout)
- [`backgroundTimeout()`](#backgroundtimeout)
- [Known Issues](#known-issues)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->
Expand Down Expand Up @@ -172,8 +173,14 @@ After the errors plugin has been initialized on a player, a `timeout()` method i

A new timeout may be set by passing a timeout in milliseconds, e.g. `player.errors.timeout(5 * 1000)`.

Setting the timeout to `Infinity` will turn off this check.

If no argument is passed, the current timeout value is returned.

### `backgroundTimeout()`

This functions exactly like [timeout](#timeout) except the default value is 5 minutes.

## Known Issues

On iPhones, default errors are not dismissible. The video element intercepts all user interaction so error message dialogs miss the tap events. If your video is busted anyways, you may not be that upset about this.
4 changes: 3 additions & 1 deletion src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ const initPlugin = function(player, options) {
// Disable timeouts in the background altogether according to the backgroundTimeout
// option, or if the player is muted, as browsers may throttle javascript timers to
// 1 minute in that case
if (document.visibilityState === 'hidden' && (options.backgroundTimeout === Infinity || player.muted())) {
if ((document.visibilityState === 'hidden' &&
(options.backgroundTimeout === Infinity || player.muted())) ||
(document.visibilityState === 'visible' && options.timeout === Infinity)) {
return;
}

Expand Down
32 changes: 32 additions & 0 deletions test/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,38 @@ QUnit.test('timeout is disabled in background if the player is muted', function(
assert.strictEqual(errors, 0, 'did not emit an error in background after 5 minutes');
});

QUnit.test('timeout is disabled in foreground if timeout option === Infinity', function(assert) {
let errors = 0;

// Init with custom option
this.player.errors({timeout: Infinity});

this.player.on('error', function() {
errors++;
});
this.player.src(sources);

this.player.trigger('play');

this.clock.tick(1 * 1000);

assert.notOk(
this.player.hasClass('vjs-waiting'),
'the plugin does not have a spinner class because timeout is disabled'
);

// document is visible
document.visibilityState = 'visible';

this.clock.tick(45 * 1000);

assert.strictEqual(errors, 0, 'did not emit an error in background after 45 seconds');

this.clock.tick(45 * 1000);

assert.strictEqual(errors, 0, 'still did not emit an error in background after another 45 seconds');
});

QUnit.test('background/foreground timeout toggling is disabled after error has occurred', function(assert) {
let errors = 0;

Expand Down

0 comments on commit b73f160

Please sign in to comment.