Skip to content

Commit

Permalink
Prevent mousemove spam
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
download13 committed Mar 9, 2014
1 parent e7b1098 commit b7cc2a8
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1294,38 +1294,47 @@ 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
clearInterval(mouseInProgress);
// 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.
Expand Down

0 comments on commit b7cc2a8

Please sign in to comment.