This is a simple for loop that runs 80 times but only every 100 ms. Each time it runs, it pushes a Y coordinate for a character into the stillChars
state array. The stillChars
state array is mapped through to create a sequence of text elements directly underneath eachother.
See this video to see the animation
const StillChars = () => {
const [stillChars, setStillChars] = useState([]);
function addChar() {
for (let i = 0; i < 80; i++) {
setTimeout(() => {
setStillChars((pS) => [
...pS,
{
y: 4 - 0.1 * 1 * i,
death: setTimeout(() => {
setStillChars((pS) => pS.filter((c, idx) => idx !== i));
}, 2000),
},
]);
}, i * 100);
}
}
useEffect(() => {
addChar();
}, []);
return (
<>
{stillChars.map((c, i) => {
return <StillChar key={i} position={[0, c.y, 0]} i={i} />;
})}
</>
);
};
Desired behavior: The death: setTimeout
function should remove the characters like a queue instead of whatever is going on in the video/codesandbox.