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

116
Views
Run setTimeout with changing time values

I have an array of objects containing a field duration that specifies the duration that the Object should stay visible.

Each Object of array has it's own duration.

So i want to make each object appear after another with the help of setTimout.

Something like this : (onClick)

const playScenes = () => {
    const cpScenes = [...scenes];
    for(let i; i < cpScenes.length;i++){
      const timer = setTimeout(() => {     
      showScene(cpScenes[i]);                 //Show the current scene
      }, cpScenes[i].duration);              //Time changes automatically
    }
    clearTimeout(timer);
  };

The way am thinking about this is that setTimeout should block execution of for loop and then run after specified time and then the loop continues..

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

0

Neither setTimeout nor setInterval block execution. Instead what you can do is wrapping it a promise like:

async function playscenes() {
   const cpScenes = [...scenes];
   for(let i; i < cpScenes.length;i++){
      await (new Promise( (resolve) =>
         const timer = setTimeout(() => {
             showScene(cpScenes[i]);
             clearTimeout(timer);
             resolve();
         }, cpScenes[i].duration);
    })
}

Basically what this does, is for every single loop, the program will wait for the new Promise to resolve, which that happens when the timer has run out. If you don't understand asynchronous code (async/await) and promises work, I'd highly recommend that you do your research first. Although this code should not run inside an a React component since i assume that you change the state every time the setTimeout is triggered. What you could do is:

imports ...
async function playscenes(showScene){...}
let scenesPlaying = false;
export function YourComponent(...){
  ...
  if(!scenesPlaying) {
     playscenes(showScene);
     scenesPlaying = true;
  }
  ...
}
about 4 years ago · Juan Pablo Isaza Report

0

I don't really see how it is related to react, anyway, you can use recursion instead of for-loop to make it work

function playScenes(scenes) {
  if (scenes.length === 0) {
    return;
  }

  const [scene, ...rest] = scenes;
  setTimeout(() => {
    showScene(scene);
    playScenes(rest);
  }, scene.duration);
}
about 4 years ago · Juan Pablo Isaza Report

0

Here's a simple for loop based solution:

function newComputerMessage() {
    let total = 0;
    for (let i = 0; i < cpScenes.length; i++) {
        total += cpScenes[i].duration;
        setTimeout(() => showScene(cpScenes[i]), total);
    }
}

In case you want to see the working, here's a working example snippet with dummy values:

let cpScenes = [5000, 5000, 3000, 4000]

function test() {
  let total = 0;
  for (let i = 0; i < cpScenes.length; i++) {
    total += cpScenes[i];
    setTimeout(() => showScene('test'), total);
  }
}

function showScene(num) {
  console.log(num);
}

test()

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!