Skip to content

Instantly share code, notes, and snippets.

Created November 2, 2016 20:04
Show Gist options
  • Save anonymous/90b8b229e4b1282f2eeec4a101a561c9 to your computer and use it in GitHub Desktop.
Save anonymous/90b8b229e4b1282f2eeec4a101a561c9 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/cozayiluha
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style id="jsbin-css">
.tall{
height: 1000px
}
</style>
</head>
<body>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<div class="tall"></div>
<script id="jsbin-javascript">
var i = 0;
var j = 0
$(window).on('scroll', function(){
console.log('i', i++);
});
function debounce(func, wait, immediate) {
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);
};
};
var myEfficientFn = debounce(function() {
// All the taxing stuff you do
console.log('j', j++);
}, 150);
window.addEventListener('scroll', myEfficientFn);
</script>
<script id="jsbin-source-css" type="text/css">.tall{
height: 1000px
}</script>
<script id="jsbin-source-javascript" type="text/javascript">var i = 0;
var j = 0
$(window).on('scroll', function(){
console.log('i', i++);
});
function debounce(func, wait, immediate) {
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);
};
};
var myEfficientFn = debounce(function() {
// All the taxing stuff you do
console.log('j', j++);
}, 150);
window.addEventListener('scroll', myEfficientFn);</script></body>
</html>
.tall{
height: 1000px
}
var i = 0;
var j = 0
$(window).on('scroll', function(){
console.log('i', i++);
});
function debounce(func, wait, immediate) {
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);
};
};
var myEfficientFn = debounce(function() {
// All the taxing stuff you do
console.log('j', j++);
}, 150);
window.addEventListener('scroll', myEfficientFn);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment