Skip to content

Instantly share code, notes, and snippets.

@thinog
Created June 14, 2024 13:41
Show Gist options
  • Save thinog/9bb3592c6768c1996a50df03fefe9312 to your computer and use it in GitHub Desktop.
Save thinog/9bb3592c6768c1996a50df03fefe9312 to your computer and use it in GitHub Desktop.
Script minified in https://github.com/thinog/bad-web-vitals-workshop to force people to defer the execution of non-critical scripts.
async function run() {
const delayMs = 1000;
const startedAt = Date.now();
console.log("[Workshop] Starting a third-party script not so important 😃");
if (document.currentScript.defer) {
await new Promise((resolve) => setTimeout(resolve, delayMs));
} else {
const waitUntil = Date.now() + delayMs;
while (Date.now() < waitUntil) continue;
}
const finishedAt = Date.now() - startedAt;
console.log(
`[Workshop] The not-so-important third-party script has been executed! It only took ${finishedAt} ms 🙏`,
);
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment