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.