I'm attempting to:
All my attempts so far have unfortunately not worked, I'm a beginner to JS so am taking snippets and trying my best here:
// var LS = document.querySelector('textarea').value().split('\n');
for (let i in LS) {
setTimeOut(function(i),5000);
}
textInput = document.getElementById("theText");
textArray = textInput.value.split(/\n/g);
const delayLoop = (fn, delay) => {
return (line, i) => {
setTimeout(() => {
useTheText(line);
}, i * 5000);
}
};
function useTheText(line){
console.log(line);
}
const handleClick = () => {
textInput.value.split(/\n/g).forEach(delayLoop(useTheText, 500));
}
<textarea id="theText" name="theText"
rows="5" cols="33">
It was a dark and stormy night...
second line
line 3
</textarea>
<div>
<button onClick="handleClick()">process</button>
</div>
The first argument to setTimeout() must be a function to call. You're calling the function immediately.
If you want them to run 5 seconds apart, multiply the timeout value by i. The timeout is measured from when you call setTimeout, not when the previous function ran.
for (let i in LS) {
setTimeout(() => yourFunction(LS[i]), i * 5000);
}