0

Update:

It may look like similar with Sleep in Javascript - delay between actions But It's actually not .In that question , it's just a simple code which isn't nested in a each loop and doesn't need to block the execution of its code. But in my case, I need to even block the each loop until the "else" block gets executed and then get back to the each loop.


Here is my code:

function drawNoisesRadars(noises, radars) {

    var noisesInATime = [];
    var timeIndicator = noises[0].get('NoiseTime').trim();

    $.each(noises, function (index, value) {

        if (!value) {
            return true;
        }

        var noiseTime = value.get('NoiseTime');
        if (noiseTime == timeIndicator) {
            noisesInATime.push(noises[index]);
        }
        else {
            //----------------------------------------------
            // wait for a second to draw the next one.
            window.setTimeout(function () {
                drawTimeSpecificNoisesRelatedRadars(noisesInATime, radars, timeIndicator);

                // clear noisesInATime once drawing is finished.
                clearArray(noisesInATime);

                timeIndicator = value.get('NoiseTime');

                noisesInATime.push(value);

            }, 1000 + index * 1000);
            //----------------------------------------------

        }

    });

}

in the else block, I'd like to have 1 second to execute the "else" block. But if I use setTimeout() then it creates asynchronous block which means the "if" block in the next iteration gets executed before the "else" block gets executed.

Update: The reason why I don't want my "else" block code asynchronous is because I will need the timeIndicator stay same within the scope of "drawTimeSpecificNoisesRelatedRadars()", but because of the setTimeout() makes the block asynchronous, the problem comes -> the value of timeIndicator is changed before drawTimeSpecificNoisesRelatedRadars() finished.

The reason why I'd like to block the "else" code for X seconds is: If I don't block it, the animation of a flying aircraft will look like a lightning -> first shown in one place then immediately displayed at its destination without showing the "between" places.

Sorry, it's really hard to explain my code situation.

What I want is just a method which I can simply use it like:

..........
       else {

                drawTimeSpecificNoisesRelatedRadars(noisesInATime, radars, timeIndicator);

                // clear noisesInATime once drawing is finished.
                clearArray(noisesInATime);

                //Need to be put inside a callback method as well.
                //Reset the timeIndicator
                timeIndicator = value.get('NoiseTime');

                noisesInATime.push(value);
                waitXSecond(X); // Synchronous method, it blocks the whole thread for X seconds.

        }

waitXSecond(X); // Synchronous method, it blocks the whole thread for X seconds.

Is it achievable?

Thank you.

Community
  • 1
  • 1
Franva
  • 6,565
  • 23
  • 79
  • 144
  • 1
    Nope, there is no equivalent to synchronous `sleep()` in Javascript. All the timer functions are asynchronous. If you manage to implement a synchronous workaround (e.g. busy loop), then modern browsers will warn their users your script is blocking, and will offer to kill it. – Frédéric Hamidi May 26 '14 at 14:54
  • JavaScript is single threaded and asynchronous. You have to think differently to do what you need. Be more specific about what you really want to do if you want us to help you. – Sebastien C. May 26 '14 at 14:59
  • @Franva, if you need your `else` block to run before giving control back to the loop, remove the call to `setTimeout()`. That's what is making it asynchronous in the first place. – Frédéric Hamidi May 26 '14 at 15:01
  • 1
    @Franva, it was not me who pointed out the duplicate :) – Frédéric Hamidi May 26 '14 at 15:02
  • hi @PatrickHofman , it may look like a duplication of that question, but it's not. Please see my update. – Franva May 26 '14 at 15:09

0 Answers0