From b7cc2a8a4c6e329322cc182447b7052850ed48ff Mon Sep 17 00:00:00 2001 From: Erin Date: Sun, 9 Mar 2014 13:09:52 -0700 Subject: [PATCH] Prevent mousemove spam Some platforms produce frequent mousemove events whether or not the mouse has moved. This causes the UI to always be displayed since userActive_ is always `true`. By keeping track of the last mouse position and only firing if the new position is different, we can eleminate the event spam. --- src/js/player.js | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/js/player.js b/src/js/player.js index 7e7eb22f2..53f6c19ef 100644 --- a/src/js/player.js +++ b/src/js/player.js @@ -1294,13 +1294,22 @@ vjs.Player.prototype.userActive = function(bool){ }; vjs.Player.prototype.listenForUserActivity = function(){ - var onMouseActivity, onMouseDown, mouseInProgress, onMouseUp, - activityCheck, inactivityTimeout; + var onActivity, onMouseMove, onMouseDown, mouseInProgress, onMouseUp, + activityCheck, inactivityTimeout, lastMoveX, lastMoveY; - onMouseActivity = vjs.bind(this, this.reportUserActivity); + onActivity = vjs.bind(this, this.reportUserActivity); + + onMouseMove = function(e) { + // Prevent mousemove spamming + if(e.screenX != lastMoveX || e.screenY != lastMoveY) { + lastMoveX = e.screenX; + lastMoveY = e.screenY; + onActivity(); + } + }; onMouseDown = function() { - onMouseActivity(); + onActivity(); // For as long as the they are touching the device or have their mouse down, // we consider them active even if they're not moving their finger or mouse. // So we want to continue to update that they are active @@ -1308,24 +1317,24 @@ vjs.Player.prototype.listenForUserActivity = function(){ // Setting userActivity=true now and setting the interval to the same time // as the activityCheck interval (250) should ensure we never miss the // next activityCheck - mouseInProgress = setInterval(onMouseActivity, 250); + mouseInProgress = setInterval(onActivity, 250); }; onMouseUp = function(event) { - onMouseActivity(); + onActivity(); // Stop the interval that maintains activity if the mouse/touch is down clearInterval(mouseInProgress); }; // Any mouse movement will be considered user activity this.on('mousedown', onMouseDown); - this.on('mousemove', onMouseActivity); + this.on('mousemove', onMouseMove); this.on('mouseup', onMouseUp); // Listen for keyboard navigation // Shouldn't need to use inProgress interval because of key repeat - this.on('keydown', onMouseActivity); - this.on('keyup', onMouseActivity); + this.on('keydown', onActivity); + this.on('keyup', onActivity); // Run an interval every 250 milliseconds instead of stuffing everything into // the mousemove/touchmove function itself, to prevent performance degradation.