Skip to content

Instantly share code, notes, and snippets.

@temberature
Created July 22, 2019 02:38
Show Gist options
  • Save temberature/56eef6f04ef3ee7b2e2bfa5d8376e9a8 to your computer and use it in GitHub Desktop.
Save temberature/56eef6f04ef3ee7b2e2bfa5d8376e9a8 to your computer and use it in GitHub Desktop.
idlePrefetch (originImageUrl) {
let fetched = false;
let myNonEssentialWork = deadline => {
const appendLinkTo = function (url) {
const linkTag = document.createElement('link')
linkTag.rel = 'prefetch'
linkTag.href = url
document.head.appendChild(linkTag)
}
if (deadline.timeRemaining() > 0) {
appendLinkTo(originImageUrl);
fetched = true;
}
if (!fetched) {
requestIdleCallback(myNonEssentialWork);
}
};
requestIdleCallback(myNonEssentialWork);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment