- commit
- 7c3673c5fbe04ea2db39c4d2ec5da9d0cfdd87c3
- parent
- d5af7d9a66f73ebfe9fc244cd33980901cd6849c
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2023-02-15 04:51
refactor: simplify thottle
Diffstat
| D | js/utils.js | 35 | ----------------------------------- |
| M | js/view.js | 15 | +++++++++++++-- |
2 files changed, 13 insertions, 37 deletions
diff --git a/js/utils.js b/js/utils.js
@@ -1,35 +0,0 @@1 -1 var _setTimeout = function(fn, timeout) {2 -1 if (timeout === 'animation') {3 -1 requestAnimationFrame(fn);4 -1 } else {5 -1 setTimeout(fn, timeout);6 -1 }7 -1 };8 -19 -1 export var throttle = function(fn, timeout) {10 -1 var blocked = false;11 -1 var called = false;12 -1 var nextArgs = [];13 -114 -1 var wrapper = function(...args) {15 -1 if (blocked) {16 -1 called = true;17 -1 nextArgs = args;18 -1 } else {19 -1 blocked = true;20 -1 called = false;21 -1 nextArgs = [];22 -123 -1 fn.apply(this, args);24 -125 -1 _setTimeout(() => {26 -1 blocked = false;27 -1 if (called) {28 -1 wrapper.apply(this, nextArgs);29 -1 }30 -1 }, timeout);31 -1 }32 -1 };33 -134 -1 return wrapper;35 -1 };
diff --git a/js/view.js b/js/view.js
@@ -1,4 +1,15 @@1 -1 import * as utils from './utils.js';-1 1 export var throttle = function(fn) { -1 2 var called = false; -1 3 return function() { -1 4 if (!called) { -1 5 called = true; -1 6 requestAnimationFrame(() => { -1 7 called = false; -1 8 fn.apply(this); -1 9 }); -1 10 } -1 11 }; -1 12 }; 2 13 3 14 export class View { 4 15 constructor(canvas, frame) { @@ -56,4 +67,4 @@ export class View { 56 67 } 57 68 } 58 6959 -1 View.prototype.render = utils.throttle(View.prototype.render, 'animation');-1 70 View.prototype.render = throttle(View.prototype.render);