From 3f3f313a738bd32c9fc18311cbf1e38676f77abe Mon Sep 17 00:00:00 2001 From: Jack Date: Sun, 2 Aug 2020 23:46:55 +0100 Subject: [PATCH] fixed lag on long custom texts --- public/js/script.js | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/public/js/script.js b/public/js/script.js index ee2805774..b4e17b24d 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -410,13 +410,15 @@ function initWords() { if ( config.mode == "time" || config.mode == "words" || - (config.mode == "custom" && customTextIsRandom) + config.mode == "custom" ) { // let wordsBound = config.mode == "time" ? 60 : config.words; let wordsBound = 60; if (config.showAllLines) { if (config.mode === "custom") { - wordsBound = customTextWordCount; + if (customTextIsRandom) { + wordsBound = customTextWordCount; + } } else if (config.mode != "time") { wordsBound = config.words; } @@ -424,9 +426,20 @@ function initWords() { if (config.mode === "words" && config.words < wordsBound) { wordsBound = config.words; } - if (config.mode == "custom" && customTextWordCount < wordsBound) { + if ( + config.mode == "custom" && + customTextIsRandom && + customTextWordCount < wordsBound + ) { wordsBound = customTextWordCount; } + if ( + config.mode == "custom" && + !customTextIsRandom && + customText.length < wordsBound + ) { + wordsBound = customText.length; + } } let wordset = language; if (config.mode == "custom") { @@ -436,8 +449,10 @@ function initWords() { randomWord = wordset[Math.floor(Math.random() * wordset.length)]; previousWord = wordsList[i - 1]; previousWord2 = wordsList[i - 2]; - if (config.mode == "custom" && wordset.length < 3) { + if (config.mode == "custom" && wordset.length < 3 && customTextIsRandom) { randomWord = wordset[Math.floor(Math.random() * wordset.length)]; + } else if (config.mode == "custom" && !customTextIsRandom) { + randomWord = customText[i]; } else { while ( randomWord == previousWord || @@ -465,11 +480,6 @@ function initWords() { wordsList.push(randomWord); } } - } else if (config.mode == "custom") { - // let w = customText.split(" "); - for (let i = 0; i < customText.length; i++) { - wordsList.push(customText[i]); - } } else if (config.mode == "quote") { randomQuote = quotes[Math.floor(Math.random() * quotes.length)]; let w = randomQuote.text.trim().split(" "); @@ -614,7 +624,12 @@ function addWord() { !config.showAllLines && (wordsList.length - inputHistory.length > 60 || (config.mode === "words" && wordsList.length >= config.words) || - (config.mode === "custom" && wordsList.length >= customTextWordCount)) + (config.mode === "custom" && + customTextIsRandom && + wordsList.length >= customTextWordCount) || + (config.mode === "custom" && + !customTextIsRandom && + wordsList.length >= customText.length)) ) return; let language = words[config.language]; @@ -628,8 +643,10 @@ function addWord() { .replace(/[.?!":\-,]/g, "") .toLowerCase(); - if (config.mode == "custom" && language.length < 3) { + if (config.mode == "custom" && customTextIsRandom && language.length < 3) { randomWord = language[Math.floor(Math.random() * language.length)]; + } else if (config.mode == "custom" && !customTextIsRandom) { + randomWord = customText[wordsList.length]; } else { while ( previousWordStripped == randomWord || @@ -3522,7 +3539,7 @@ $(document).keydown((event) => { if ( config.mode == "time" || config.mode == "words" || - (config.mode == "custom" && customTextIsRandom) + config.mode == "custom" ) { addWord(); }