remove 2s delay

https://codepen.io/aldrinbright/pen/ZERWQqp how can I stop the 2s delay at count 3?
8 Replies
vince
vince2y ago
Stack Overflow
How to make a setInterval stop after some time or after a number of...
I've created a loop of "changing words" with jQuery by using the code in this answer: jQuery: Find word and change every few seconds How do I stop it after some time? Say after either 60
vince
vince2y ago
let tCount = 0;

let changeText = () => {
if (tCount < 5) {
tCount++;
console.log(tCount);
return tCount;
}
};

const interval = setInterval(() => {
changeText();

if (tCount == 4) {
clearInterval(interval);
}
}, 2000);
let tCount = 0;

let changeText = () => {
if (tCount < 5) {
tCount++;
console.log(tCount);
return tCount;
}
};

const interval = setInterval(() => {
changeText();

if (tCount == 4) {
clearInterval(interval);
}
}, 2000);
Probably a better way to do this but this is working for me
Aldrin
Aldrin2y ago
i dont want to clear interval after count 3, it pauses for 2 sec, I don't want that delay
vince
vince2y ago
Then please edit your post so it makes it more clear for people https://discord.com/channels/436251713830125568/1022288836715356180
Jochem
Jochem2y ago
I think you probably still need to clear and then reset the interval with the new delay you want so vince's code is still valid, just needs an extra setInterval() in there after the clearInterval()
Aldrin
Aldrin2y ago
i am confused
MarkBoots
MarkBoots2y ago
like so?
let tCount = 0;
const changeText = () => {
console.log(tCount);
tCount < 4 ? tCount++ : tCount = 0;
if(tCount !== 3) setTimeout(changeText, 2000);
else changeText()
}

changeText()
let tCount = 0;
const changeText = () => {
console.log(tCount);
tCount < 4 ? tCount++ : tCount = 0;
if(tCount !== 3) setTimeout(changeText, 2000);
else changeText()
}

changeText()
timeout calls function again after 2000ms if not 3,
Aldrin
Aldrin2y ago
Thanks
Want results from more Discord servers?
Add your server