Skip to content

Instantly share code, notes, and snippets.

@Le-Fu
Created May 1, 2017 11:47
Show Gist options
  • Select an option

  • Save Le-Fu/04a61874bddeb39127def2d84236e430 to your computer and use it in GitHub Desktop.

Select an option

Save Le-Fu/04a61874bddeb39127def2d84236e430 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/sutuyut
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
function debounce(fn, delta, context) {
var timeoutID = null;
return function() {
clearTimeout(timeoutID);
var args = arguments;
timeoutID = setTimeout(function() {
fn.apply(context, args);
}, delta);
};
}
function immediate(fn, delta, context) {
var timeoutID = null;
var safe = true;
return function() {
var args = arguments;
if (safe) {
fn.call(context, args);
safe = false;
}
clearTimeout(timeoutID);
timeoutID = setTimeout(function() {
safe = true;
}, delta);
};
}
function throttle(fn, delta, context) {
var safe = true;
return function() {
var args = arguments;
if (safe) {
fn.call(context, args);
safe = false;
setTimeout(function() {
safe = true;
}, delta);
}
};
}
function D() {
document.body.innerHTML += 'D<br>';
}
function I() {
document.body.innerHTML += 'I<br>';
}
function T() {
document.body.innerHTML += 'T<br>';
}
window.addEventListener('mousemove', throttle(T, 1000));
window.addEventListener('mousemove', debounce(D, 1000));
window.addEventListener('mousemove', immediate(I, 1000));
</script>
<script id="jsbin-source-javascript" type="text/javascript">function debounce(fn, delta, context) {
var timeoutID = null;
return function() {
clearTimeout(timeoutID);
var args = arguments;
timeoutID = setTimeout(function() {
fn.apply(context, args);
}, delta);
};
}
function immediate(fn, delta, context) {
var timeoutID = null;
var safe = true;
return function() {
var args = arguments;
if (safe) {
fn.call(context, args);
safe = false;
}
clearTimeout(timeoutID);
timeoutID = setTimeout(function() {
safe = true;
}, delta);
};
}
function throttle(fn, delta, context) {
var safe = true;
return function() {
var args = arguments;
if (safe) {
fn.call(context, args);
safe = false;
setTimeout(function() {
safe = true;
}, delta);
}
};
}
function D() {
document.body.innerHTML += 'D<br>';
}
function I() {
document.body.innerHTML += 'I<br>';
}
function T() {
document.body.innerHTML += 'T<br>';
}
window.addEventListener('mousemove', throttle(T, 1000));
window.addEventListener('mousemove', debounce(D, 1000));
window.addEventListener('mousemove', immediate(I, 1000));</script></body>
</html>
function debounce(fn, delta, context) {
var timeoutID = null;
return function() {
clearTimeout(timeoutID);
var args = arguments;
timeoutID = setTimeout(function() {
fn.apply(context, args);
}, delta);
};
}
function immediate(fn, delta, context) {
var timeoutID = null;
var safe = true;
return function() {
var args = arguments;
if (safe) {
fn.call(context, args);
safe = false;
}
clearTimeout(timeoutID);
timeoutID = setTimeout(function() {
safe = true;
}, delta);
};
}
function throttle(fn, delta, context) {
var safe = true;
return function() {
var args = arguments;
if (safe) {
fn.call(context, args);
safe = false;
setTimeout(function() {
safe = true;
}, delta);
}
};
}
function D() {
document.body.innerHTML += 'D<br>';
}
function I() {
document.body.innerHTML += 'I<br>';
}
function T() {
document.body.innerHTML += 'T<br>';
}
window.addEventListener('mousemove', throttle(T, 1000));
window.addEventListener('mousemove', debounce(D, 1000));
window.addEventListener('mousemove', immediate(I, 1000));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment