Guest User

Untitled

a guest
Jan 21st, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. var requestAnimationFrame = (
  2. window.requestAnimationFrame ||
  3. window.webkitRequestAnimationFrame ||
  4. window.mozRequestAnimationFrame ||
  5. window.oRequestAnimationFrame ||
  6. function(fn) { return setTimeout(fn, 1000/60); }
  7. ),
  8. animationLoop = function next(fn, immediate) {
  9. immediate && (fn() === false) || requestAnimationFrame(next.bind(null, fn, true));
  10. };
  11.  
  12. function debounce(fn, threshhold) {
  13. threshhold = threshhold || 1000/60;
  14. var timer;
  15. return function(){
  16. var run = fn.apply.bind(fn, this, arguments);
  17. clearTimeout(timer);
  18. timer = setTimeout(run, threshhold);
  19. };
  20. }
  21.  
  22. function throttle (fn, threshhold) {
  23. var next = function(fn) { setTimeout(fn, threshhold); };
  24. // Wenn threshhold nicht angegeben, mit Bildshirm synchronizieren
  25. if (!threshhold) {
  26. threshhold = 1000/60;
  27. next = window.requestAnimationFrame ||
  28. window.webkitRequestAnimationFrame ||
  29. window.mozRequestAnimationFrame ||
  30. window.oRequestAnimationFrame ||
  31. next;
  32. }
  33.  
  34. var waiting = false,
  35. runAfterWait = false,
  36. run;
  37. return function() {
  38. run = fn.apply.bind(fn, this, arguments);
  39.  
  40. if (waiting) {
  41. runAfterWait = true;
  42. } else {
  43. run();
  44. waiting = true;
  45. next(function() {
  46. waiting = false;
  47. if (runAfterWait) {
  48. runAfterWait = false;
  49. run();
  50. }
  51. });
  52. }
  53. };
  54. }
  55.  
  56. function memoize(fn) {
  57. var cache = {};
  58. return function() {
  59. var key = JSON.stringify(arguments);
  60. if (!cache.hasOwnProperty(key)) {
  61. cache[key] = fn.apply(this, arguments);
  62. }
  63. return cache[key];
  64. }
  65. }
Add Comment
Please, Sign In to add comment