Last active
September 13, 2018 09:31
-
-
Save okj579/65cafce2674db475b4946e562f7ba1a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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