Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

215
Views
How can I make this interval stop when it reaches the end of the array?

I am trying to make a little animation with an array where it kind of types out my name like in a command prompt. This is my first time using hooks in react so I'm kind of lost. I'd like to make it so that the interval stops at "Felipe Garcia Diaz" but instead of it stopping the interval just makes my text disappear and it continues to run in the background. What am i doing wrong exactly?

HooksExample.jsx

import React, {useState} from 'react';
import socket from './socket';

function Main(props) {
    let introArray = ["_","F_", "Fe_", "Fel_", "Feli_", "Felip_", "Felipe_", "Felipe _", "Felipe G_", "Felipe Ga_", "Felipe Gar_", "Felipe Garc_", "Felipe Garci_", "Felipe Garcia_"];

    let [myName, setName] = useState(introArray);
    let [climber, setClimber] = useState(0);

    let block = false;
    if(block === false) {
        let climbArray = () => {
            setClimber(climber += 1);
            if(climber === introArray.length) {
                block = true;
                console.log('dead');
            }
            console.log('live', climber);
        }
        setInterval(climbArray, 200);
    }
    return (
        <React.Fragment>
            <h1>{myName[climber]}</h1>
        </React.Fragment>
    );
}


export default Main;
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Some feedback:

  • to clear an interval, you need to store the value returned from setInterval() and pass it into clearInterval()
  • triggering an interval and updating state are side effects so should be wrapped inside useEffect()
  • for useEffect() to access latest climber value, it must be in the dependency array like [climber]
  • myName does not need to be in state because its value can be derived from introArray
  • I replaced introArray with string for simplicity's sake, if you really want to use array then corresponding JSX is <h1>{introArray[climber]}</h1>
import React, {useState, useEffect} from 'react';
import "./styles.css";

export default function App() {
  const intro = "Felipe Garcia_";
  const [climber, setClimber] = useState(0);

  useEffect(()=>{
    const interval = setInterval(() => {
      if (climber === intro.length) {
        console.log('clearing');
        clearInterval(interval);
      } else {
        setClimber((val) => val + 1);
      }
    }, 200);
    // clean up interval on unmount
    return (()=>{
      clearInterval(interval);
    });
  }, [climber]);

  return (
      <React.Fragment>
        <h1>
          {intro.substring(0, climber)}_
        </h1>
      </React.Fragment>
  );
}

Working example

about 4 years ago · Juan Pablo Isaza Report

0

import React, {useState} from 'react';
import socket from './socket';

function Main(props) {
    let introArray = ["_","F_", "Fe_", "Fel_", "Feli_", "Felip_", "Felipe_", "Felipe _", "Felipe G_", "Felipe Ga_", "Felipe Gar_", "Felipe Garc_", "Felipe Garci_", "Felipe Garcia_"];
let interval 
    let [myName, setName] = useState(introArray);
    let [climber, setClimber] = useState(0);

    let block = false;
    if(block === false) {
        let climbArray = () => {
            setClimber(climber += 1);
            if(climber === introArray.length) {
                block = true;
                if(interval !== undefined){
                  clearInterval(interval)
                }
              
                console.log('dead');
                
            }
            console.log('live', climber);
        }
        interval = setInterval(climbArray, 200);
    }
    return (
        <React.Fragment>
            <h1>{myName[climber]}</h1>
        </React.Fragment>
    );
}


export default Main;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!