123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <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)







            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console