Created
July 27, 2022 16:42
-
-
Save chamatt/04b2726623f44e6d2be099a4f94c800c to your computer and use it in GitHub Desktop.
Get optimized timeout for human reading speed using the text size
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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