Skip to content

Instantly share code, notes, and snippets.

@matallo
Forked from timc1/getDelayFromNetworkSpeed.ts
Created April 25, 2020 18:49
Show Gist options
  • Save matallo/58f4052cf112f19645ce6c13b51e3050 to your computer and use it in GitHub Desktop.
Save matallo/58f4052cf112f19645ce6c13b51e3050 to your computer and use it in GitHub Desktop.
Control the speed at which your loading state shows up depending on the user's internet speed.
const defaultDelay = 500;
export default function getDelay(): number {
if (typeof window !== "undefined") {
if (window.navigator && window.navigator.connection) {
const connection = window.navigator.connection.effectiveType;
switch (connection) {
case "4g":
return defaultDelay;
case "3g":
return 200;
case "2g":
return 0;
default:
return defaultDelay;
}
}
}
return defaultDelay;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment