Skip to content

Instantly share code, notes, and snippets.

@okj579
Last active September 13, 2018 09:31
Show Gist options
  • Save okj579/65cafce2674db475b4946e562f7ba1a7 to your computer and use it in GitHub Desktop.
Save okj579/65cafce2674db475b4946e562f7ba1a7 to your computer and use it in GitHub Desktop.
var requestAnimationFrame = (
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
function(fn) { return setTimeout(fn, 1000/60); }
),
animationLoop = function next(fn, immediate) {
immediate && (fn() === false) || requestAnimationFrame(next.bind(null, fn, true));
};
function debounce(fn, threshhold) {
threshhold = threshhold || 1000/60;
var timer;
return function(){
var run = fn.apply.bind(fn, this, arguments);
clearTimeout(timer);
timer = setTimeout(run, threshhold);
};
}
function throttle (fn, threshhold) {
// Wenn threshhold nicht angegeben, mit Bildshirm synchronizieren
var next = threshhold ? function(fn) { setTimeout(fn, threshhold) } : requestAnimationFrame;
var waiting = false,
runAfterWait = false,
run;
return function() {
run = fn.apply.bind(fn, this, arguments);
if (waiting) {
runAfterWait = true;
} else {
run();
waiting = true;
next(function() {
waiting = false;
if (runAfterWait) {
runAfterWait = false;
run();
}
});
}
};
}
function memoize(fn) {
var cache = {};
return function() {
var key = JSON.stringify(arguments);
if (!cache.hasOwnProperty(key)) {
cache[key] = fn.apply(this, arguments);
}
return cache[key];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment