diff --git a/timer/plugin.js b/timer/plugin.js index 47948c7..5c6e3bf 100644 --- a/timer/plugin.js +++ b/timer/plugin.js @@ -1,19 +1,30 @@ import Inspire from "@inspirejs/core"; +export const hasCSS = true; + Inspire.hooks.add("init-start", me => { // Create timer, if needed - this.duration = document.body.getAttribute("data-duration"); + let duration = document.body.getAttribute("data-duration"); - if (this.duration > 0) { + if (duration > 0) { document.body.insertAdjacentHTML( "beforeend", - `
`, + `
`, ); - addEventListener("load", evt => { + // Start the countdown on load — or right away if the plugin loaded after + // the load event already fired, so a late load isn't missed. + let start = () => { timer.className = "end"; - setTimeout(() => timer.classList.add("overtime"), this.duration * 60000); - }); + setTimeout(() => timer.classList.add("overtime"), duration * 60000); + }; + + if (document.readyState === "complete") { + start(); + } + else { + addEventListener("load", start); + } } });