Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function debounce(fn, debounceTime) {
- if (typeof fn !== "function") {
- throw "debounce arg is not a function";
- }
- var time = debounceTime || 300;
- var timeout;
- return function() {
- if (timeout) {
- clearTimeout(timeout);
- timeout = undefined;
- }
- timeout = setTimeout(fn, time);
- };
- }
- function throttle(fn, throttleTime) {
- if (typeof fn !== "function") {
- throw "throttle arg is not a function";
- }
- var time = throttleTime || 300;
- var scheduled = -1; // initial = -1, set - 1, not set = 0
- return function() {
- if (scheduled === 1) {
- return;
- }
- setTimeout(function() {
- fn();
- scheduled = 0;
- }, time);
- scheduled = 1;
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement