Skip to content

Instantly share code, notes, and snippets.

@KonstKh
Created December 12, 2017 10:31
Show Gist options
  • Save KonstKh/f0bb96cdc6601720b081d288c70b1719 to your computer and use it in GitHub Desktop.
Save KonstKh/f0bb96cdc6601720b081d288c70b1719 to your computer and use it in GitHub Desktop.
JS Bin [throttle scroll event] // source http://jsbin.com/mayanujeyo
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="[throttle scroll event]">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style id="jsbin-css">
.big{
color: red;
outline: 1px solid blue;
width: 100px;
height: 300px;
}
</style>
</head>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<body id="body">
<div class="big">1</div>
<div class="big">2</div>
<div class="big">3</div>
<div class="big">4</div>
<div class="big">5</div>
<div class="big">6</div>
<div class="big">7</div>
<div class="big">8</div>
<div class="big">9</div>
<div class="big">0</div>
<script id="jsbin-javascript">
var delay = 20;
var timeout = null;
$(window).bind('scroll',function(){
clearTimeout(timeout);
timeout = setTimeout(function(){
console.log('scrolling stopped');
},delay);
});
// too often
// window.addEventListener('scroll', function()
// {
// console.log('scrolled');
// });
</script>
<script id="jsbin-source-css" type="text/css">.big{
color: red;
outline: 1px solid blue;
width: 100px;
height: 300px;
}</script>
<script id="jsbin-source-javascript" type="text/javascript">
var delay = 20;
var timeout = null;
$(window).bind('scroll',function(){
clearTimeout(timeout);
timeout = setTimeout(function(){
console.log('scrolling stopped');
},delay);
});
// too often
// window.addEventListener('scroll', function()
// {
// console.log('scrolled');
// });
</script></body>
</html>
.big{
color: red;
outline: 1px solid blue;
width: 100px;
height: 300px;
}
var delay = 20;
var timeout = null;
$(window).bind('scroll',function(){
clearTimeout(timeout);
timeout = setTimeout(function(){
console.log('scrolling stopped');
},delay);
});
// too often
// window.addEventListener('scroll', function()
// {
// console.log('scrolled');
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment