Add use.whenReady function

This commit is contained in:
Talia 2021-11-03 20:51:17 +01:00
parent 797b6448c8
commit 34cac3913f
No known key found for this signature in database
GPG Key ID: AD727AD22802D0D6
2 changed files with 15 additions and 4 deletions

7
use.js
View File

@ -32,6 +32,13 @@ export const run = (root = document) => {
root.querySelectorAll("[use]").forEach(use)
}
export const whenReady = () => {
if (document.readyState == "complete")
run(document)
else
document.addEventListener("readystatechange", whenReady, {once: true})
}
export const install = () => {
observe(document)
run(document)

12
use.md
View File

@ -43,11 +43,15 @@ use.observe(root=document)
// child elements. Does not perform an initial run over
// already existing elements.
use.use(element)
// Runs once on a selected element.
// Normally not recommended.
use.install()
// Observes the entire document and performs
// an initial scan over existing elements.
use.whenReady()
// Waits for the document to be ready, then
// runs once on the whole document.
use.use(element)
// Runs once on a selected element.
// Normally not recommended.
```