Skip to content

Instantly share code, notes, and snippets.

@chamatt
Created July 27, 2022 16:42
Show Gist options
  • Save chamatt/04b2726623f44e6d2be099a4f94c800c to your computer and use it in GitHub Desktop.
Save chamatt/04b2726623f44e6d2be099a4f94c800c to your computer and use it in GitHub Desktop.
Get optimized timeout for human reading speed using the text size
// minimum timeout
const BASE_HIDE_TIMEOUT = 3000 //ms
export const getFlashTimeout = (message: string) => {
const averageWordsPerSecond = 4 // average number of words per second a human can read
const amountOfWords = message.split(" ").length
const idealTimeout = (amountOfWords / averageWordsPerSecond) * 1000
// factor in reading pauses for longer sentences
const factor = Math.floor(amountOfWords / 10)
const readingPause = 500 * 1.2 ** factor
return Math.floor(Math.max(BASE_HIDE_TIMEOUT, idealTimeout + readingPause))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment