<div class="all">
  <div>
  Regular handler: <span class="regular"></span>
  </div>
  <div>
  Debounce handler: <span class="debounce"></span>
  </div>
  <div>
  Throttle handler: <span class="throttle"></span>
  </div>
  <div>
  Combo handler: <span class="combo"></span>
  </div>
</div>
/*

Combine throttling and debounce for an even better UX.

If you move your mouse above the read area below,
you will see the numbers update at different intervals.

The Regular handler update the x, y axis for every mousemove event.

The Debounce version only update the
number when it has been 50ms since the event was last seen.

The throttle handler update every 250 ms, while the mouse is moving.

I wanted something like throttle, so the event handler isn't run more than needed (only once per 250 ms). But I also wanted the GUI to update as fast as possible when events stopped emitting (for instance 50ms). I managed this by combining the throttle and debounce functions.

In the js I just called the function 'combo'. Can you figure out an even better name?

*/

body {
  margin: 0;
  padding: 0;
}

.all {
  padding: 20px;
  height: 100vh;
  width: 100vw;
}
var all = document.querySelector('.all'),
    reg = document.querySelector('.regular'),
    deb = document.querySelector('.debounce'),
    thr = document.querySelector('.throttle'),
    com = document.querySelector('.combo');

// Simplest, dont run once when event haven been seen for 50 ms
// You have to wait until events end firing before function is run
function debounce(method, delayMs) {
	delayMs = delayMs || 50;
	var timer = null;
	return function() {
		var context = this, args = arguments;
		clearTimeout(timer);
		timer = setTimeout(function() {
			method.apply(context, args);
		}, delayMs);
	};
}

// Somewhat more complicated. function is fired right away, then for maximum
// every 250 ms. You might potentially have to wait 250 ms after last seen event
function throttle(fn, threshold) {
	threshold = threshold || 250;
	var last, deferTimer;

	return function() {
		var now = +new Date, args = arguments;
		if(last && now < last + threshold) {
			clearTimeout(deferTimer);
			deferTimer = setTimeout(function() {
				last = now;
				fn.apply(this, args);
			}, threshold);
		} else {
			last = now;
			fn.apply(this, args);
		}
	}
}

// Somewhat more complicated. function is fired right away, then for maximum
// every 250 ms. You might potentially have to wait 250 ms after last seen event
function combo(fn, threshold) {
	threshold = threshold || 250;
	var last, deferTimer;
  
  var db = debounce(fn)
	return function() {
		var now = +new Date, args = arguments;
		if(!last || (last && now < last + threshold)) {
			clearTimeout(deferTimer);
      db.apply(this, args);
			deferTimer = setTimeout(function() {
				last = now;
				fn.apply(this, args);
			}, threshold);
		} else {
			last = now;
			fn.apply(this, args);
		}
	}
}

// I want a combination of the above. Run handler right away, then every 250ms
// but only wait 50 ms if event are no longer seen (for an even more snappy UX)

function handler(e) {	
  reg.textContent = `${e.clientX}, ${e.clientY}`;
}

var db = debounce(function handler(e) {	
  deb.textContent = `${e.clientX}, ${e.clientY}`;
}, 1000)

var dt = throttle(function handler(e) {	
  thr.textContent = `${e.clientX}, ${e.clientY}`;
}, 5000)

var co = combo(function handler(e) {
  com.textContent = `${e.clientX}, ${e.clientY}`;
}, 5000)

all.addEventListener('mousemove', handler)
all.addEventListener('mousemove', db)
all.addEventListener('mousemove', dt)
all.addEventListener('mousemove', co)






External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.