From 5a7e74eb8be3008fedd88c53a0a8411f035beb49 Mon Sep 17 00:00:00 2001 From: billyzou Date: Sun, 21 Apr 2019 18:34:44 -0400 Subject: [PATCH] feat: add a function to throttle scrolling events --- src/remark/controllers/inputs/mouse.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/remark/controllers/inputs/mouse.js b/src/remark/controllers/inputs/mouse.js index 42cc8e69..ea62dce1 100644 --- a/src/remark/controllers/inputs/mouse.js +++ b/src/remark/controllers/inputs/mouse.js @@ -6,6 +6,17 @@ exports.unregister = function (events) { removeMouseEventListeners(events); }; +function throttle(events, eventName) { + var timestamp = null; + return function() { + var now = Date.now(); + if (timestamp === null || now - timestamp > 100) { + events.emit(eventName); + } + timestamp = now; + }; +} + function addMouseEventListeners (events, options) { if (options.click) { events.on('click', function (event) { @@ -27,13 +38,15 @@ function addMouseEventListeners (events, options) { }); } + var throttledPrev = throttle(events, 'gotoPreviousSlide'); + var throttledNext = throttle(events, 'gotoNextSlide'); if (options.scroll !== false) { var scrollHandler = function (event) { if (event.wheelDeltaY > 0 || event.detail < 0) { - events.emit('gotoPreviousSlide'); + throttledPrev(); } else if (event.wheelDeltaY < 0 || event.detail > 0) { - events.emit('gotoNextSlide'); + throttledNext(); } };