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

98
Views
React double values, useEffect and setInterval

Why do I have double values? I am using useEffect and the setInterval function. In the setInterval function there is a single increment or decrement and after a second there is another one. Why am I getting a double value in the results? How to change it?

  const demo = "1 2 3 4 5";
  let i = 0;  
  let j = demo.length;
  useEffect(() => {
    const interval = setInterval(() => {
      if (i < demo.length) {
        console.log(demo.substring(0, i));
        i++;
      } else if (j > 0) {
        console.log(demo.substring(0, j));
        j--;
      }
    }, 1000);
  }, [i, j]);

Result:

1
1 
1 2
1 2 
1 2 3
1 2 3 
1 2 3 4
1 2 3 4 
1 2 3 4 5
1 2 3 4 
1 2 3 4
1 2 3 
1 2 3
1 2 
1 2
1 
1  
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are not getting double values, you are getting the spaces in the demo string.

You results is

'1'
'1 '
'1 2'
'1 2 '
'1 2 3'
'1 2 3 '
'1 2 3 4'
'1 2 3 4 '
'1 2 3 4 5'
'1 2 3 4 '
'1 2 3 4'
'1 2 3 '
'1 2 3'
'1 2 '
'1 2'
'1 '
'1'
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!