mirror of
https://github.com/videojs/video.js.git
synced 2025-07-17 01:42:41 +02:00
fix(throttle): Fix error in Fn.throttle that broke MouseTimeDisplay (#3833)
We were not initializing `last` properly in the throttle function.
This commit is contained in:
committed by
Gary Katsevman
parent
766580af2b
commit
014c6b89e6
@ -55,7 +55,7 @@ export const bind = function(context, fn, uid) {
|
|||||||
* @return {Function}
|
* @return {Function}
|
||||||
*/
|
*/
|
||||||
export const throttle = function(fn, wait) {
|
export const throttle = function(fn, wait) {
|
||||||
let last;
|
let last = Date.now();
|
||||||
|
|
||||||
const throttled = function(...args) {
|
const throttled = function(...args) {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
@ -1,7 +1,15 @@
|
|||||||
/* eslint-env qunit */
|
/* eslint-env qunit */
|
||||||
|
import sinon from 'sinon';
|
||||||
import * as Fn from '../../../src/js/utils/fn.js';
|
import * as Fn from '../../../src/js/utils/fn.js';
|
||||||
|
|
||||||
QUnit.module('fn');
|
QUnit.module('fn', {
|
||||||
|
beforeEach() {
|
||||||
|
this.clock = sinon.useFakeTimers();
|
||||||
|
},
|
||||||
|
afterEach() {
|
||||||
|
this.clock.restore();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
QUnit.test('should add context to a function', function(assert) {
|
QUnit.test('should add context to a function', function(assert) {
|
||||||
const newContext = { test: 'obj'};
|
const newContext = { test: 'obj'};
|
||||||
@ -12,3 +20,28 @@ QUnit.test('should add context to a function', function(assert) {
|
|||||||
|
|
||||||
fdsa();
|
fdsa();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test('should throttle functions properly', function(assert) {
|
||||||
|
const tester = sinon.spy();
|
||||||
|
const throttled = Fn.throttle(tester, 100);
|
||||||
|
|
||||||
|
// We must wait a full wait period before the function can be called.
|
||||||
|
this.clock.tick(100);
|
||||||
|
throttled();
|
||||||
|
throttled();
|
||||||
|
this.clock.tick(50);
|
||||||
|
throttled();
|
||||||
|
|
||||||
|
assert.strictEqual(tester.callCount, 1, 'the throttled function has been called the correct number of times');
|
||||||
|
|
||||||
|
this.clock.tick(50);
|
||||||
|
throttled();
|
||||||
|
|
||||||
|
assert.strictEqual(tester.callCount, 2, 'the throttled function has been called the correct number of times');
|
||||||
|
|
||||||
|
throttled();
|
||||||
|
this.clock.tick(100);
|
||||||
|
throttled();
|
||||||
|
|
||||||
|
assert.strictEqual(tester.callCount, 3, 'the throttled function has been called the correct number of times');
|
||||||
|
});
|
||||||
|
Reference in New Issue
Block a user