Implement lazy loading for Audio and Video

This commit is contained in:
Michael Fabian 'Xaymar' Dirks
2022-01-12 05:53:50 +01:00
parent 4a70741419
commit 0d03a01a9f
4 changed files with 44 additions and 2 deletions
+26
View File
@@ -6,4 +6,30 @@
document.querySelector("#header #navigation").classList.toggle("open");
});
{ // Lazily load Media
let info = {}
new Promise((resolve, reject) => {
// Figure out what to load.
info.elements = document.querySelectorAll(".block-media > [data-lazyload]");
info.stack = Array.from(info.elements);
console.debug("Lazily loading " + info.stack.length + " elements...");
// Begin processing at 1ms intervals.
info.cbid = 0;
info.cbid = window.setInterval(() => {
let el = info.stack.pop();
let lazycontent = el.querySelector("noscript");
el.innerHTML = lazycontent.innerHTML;
if (info.stack.length == 0) {
window.clearInterval(info.cbid);
resolve();
}
}, 1);
}).finally(() => {
console.debug("Lazy loading complete.");
window.clearInterval(info.cbid);
});
}
})();