Skip to content

Instantly share code, notes, and snippets.

@timc1
Last active March 15, 2022 08:13
Show Gist options
  • Save timc1/6f0aac4f9e5a41f33202842b04e7d054 to your computer and use it in GitHub Desktop.
Save timc1/6f0aac4f9e5a41f33202842b04e7d054 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;
}
@StarpTech
Copy link

I would make it dependent on how long the call really needs. If a call takes less 150ms it doesn't matter if it is on 2g or 4g.

@timc1
Copy link
Author

timc1 commented Apr 25, 2020

Good call, thank you! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment