/*
* David Walsh's debounce function with optional leader argument; function passed to leader
* will execute with every call and ignore debounce
* http://davidwalsh.name/javascript-debounce-function
* debounce( function func, int wait, bool immediate, function leader )
* func(): Function you want to debounce
* wait: Time to wait in milliseconds
* immediate: If true, executes function at leading edge of wait
* leader: Function to be executed with each call, ignoring debounce
*/
var debounce = function(func, wait, immediate, leader) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) {
func.apply(context, args);
}
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) {
func.apply(context, args);
}
if (leader) {
leader.apply(context, args);
}
};
};
$('.box').on('mousemove', debounce(function(e) {
$('.debounced').html(e.pageX, e.pageY);
},
250,
false,
function(e) {
$('.leader').html(e.pageX, e.pageY);
}
));
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.