A
A
Alexander Belyakov2017-02-24 16:36:38
JavaScript
Alexander Belyakov, 2017-02-24 16:36:38

How to make a function wait in JavaScript?

The loop forcalls a function imitate(j)that performs an animation in WebGL and lasts 24 seconds.
Cycle:

for (j; j<=finDay; j++) { 
  imitate(j); 
};

Function:
imitate = function(d) { 				
        
          function startTrackInterval(day) {
            if (day) effectController.day=day;
            effectController.hour=0;
            var timerId = setInterval(function() {
            if (effectController.hour > 24) clearInterval(timerId);
            else {
            gui.updateDisplay();
            tracker();
            };
            effectController.hour=effectController.hour+0.01;
            }, 10);
            }

          startTrackInterval(d);
            
        }

How to make in a loop waiting for the execution of a function and only then increment j?
If this is not done, then JS runs finDaythe -th number of copies of the function almost in parallel, and the rendering turns out to be a mess.
PS: I read about callbacks and promises, but I couldn't apply them.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2017-02-24
@vshvydky

If a function with a callback needs to be wrapped in a promise, in order to work with promises synchronously, you must either use a recursive call to promises one after another, or in a for loop in an async function with interrupts on alert

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question