szurubooru/client/js/util/optimized_resize.js

34 lines
645 B
JavaScript
Raw Normal View History

'use strict';
let callbacks = [];
let running = false;
function resize() {
if (!running) {
running = true;
if (window.requestAnimationFrame) {
window.requestAnimationFrame(runCallbacks);
} else {
setTimeout(runCallbacks, 66);
}
}
}
function runCallbacks() {
2020-06-04 20:09:35 +02:00
callbacks.forEach(callback => {
callback();
});
running = false;
}
function add(callback) {
callbacks.push(callback);
}
function remove(callback) {
callbacks = callbacks.filter(c => c !== callback);
}
window.addEventListener('resize', resize);
module.exports = {add: add, remove: remove};