console
var restArgs = function (func, startIndex) {
startIndex = startIndex == null ? func.length - 1 : +startIndex;
return function () {
var length = Math.max(arguments.length - startIndex, 0);
var rest = Array(length);
for (var index = 0; index < length; index++) {
rest[index] = arguments[index + startIndex];
}
switch (startIndex) {
case 0:
return func.call(this, rest);
case 1:
return func.call(this, arguments[0], rest);
case 2:
return func.call(this, arguments[0], arguments[1], rest);
}
var args = Array(startIndex + 1);
for (index = 0; index < startIndex; index++) {
args[index] = arguments[index];
}
args[startIndex] = rest;
return func.apply(this, args);
};
};
var throttle = function (func, wait, options) {
var timeout, context, args, result;
var previous = 0;
if (!options) options = {};
var later = function () {
previous = options.leading === false ? 0 : new Date();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};
var throttled = function () {
var now = new Date();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
throttled.cancel = function () {
clearTimeout(timeout);
previous = 0;
timeout = context = args = null;
};
return throttled;
};
var debounce = function (func, wait, immediate) {
var timeout, result;
var later = function (context, args) {
timeout = null;
if (args) result = func.apply(context, args);
};
var debounced = restArgs(function (args) {
if (timeout) clearTimeout(timeout);
if (immediate) {
var callNow = !timeout;
timeout = setTimeout(later, wait);
if (callNow) result = func.apply(this, args);
} else {
timeout =restArgs(function (func, wait, args) {
return setTimeout(function () {
return func.apply(null, args);
}, wait);
})(later, wait, this, args);
}
return result;
});
debounced.cancel = function () {
clearTimeout(timeout);
timeout = null;
};
return debounced;
};
var inside2 = document.querySelector(".inside-2");
var thing2 = document.querySelector(".thing-2");
var count2 = document.querySelector(".count-2");
inside2.addEventListener('scroll',throttle(function() {
thing2.style.Top=inside2.scrollTop;
count2.innerHTML=parseInt(count2.innerHTML)+1;
}, 150),false);
var inside3 = document.querySelector(".inside-3");
var thing3 = document.querySelector(".thing-3");
var count3 = document.querySelector(".count-3");
inside3.addEventListener('scroll',debounce(function() {
thing3.style.Top=inside3.scrollTop;
count3.innerHTML=parseInt(count3.innerHTML)+1;
}, 150),false);
<div class="area area-2">
<div class="inside inside-2">
<div class="content content-2"></div>
<div class="thing thing-2">Throttled</div>
</div>
<div class="count count-2">0</div>
</div>
<div class="area area-3">
<div class="inside inside-3">
<div class="content content-3"></div>
<div class="thing thing-3">Debounced</div>
</div>
<div class="count count-3">0</div>
</div>
* {
box-sizing: border-box;
}
body {
background: #eee;
}
.area {
width: 300px;
height: 200px;
margin: 20px auto;
background: white;
position: relative;
}
.inside {
height: 200px;
position: relative;
overflow: auto;
}
.content {
height: 5000px;
}
.thing {
position: absolute;
top: 0;
left: 0;
width: 100%;
text-align: center;
background: #f06d06;
color: white;
padding: 10px;
}
.count {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}