I have the below code with the if condition
if(oldMembership++ <= newMembership) {
var digit;
$('ul#indexSiteCounterBottom').empty();
for(i = 0; i < 9; i++) {
if(membership.toString()[i] == '_') {
digit = ' ';
} else {
digit = membership.toString()[i];
}
$('ul#indexSiteCounterBottom').append('<li>'+digit+'</li>');
$('ul#indexSiteCounterBottom li:nth-child(3n)').addClass('extra-margin');
}
}
If the ‘if’ condition is meet the rest of the code is run.
I want to be able to slow the running of the below code by around 500ms for each loop of the ‘if’.
I’ve tried to put in setInterval and setTimeout but I haven’t used them before and the ‘if’ condition completed all loops instantly.
How can I add setInterval or SetTimeout to this so each ‘if’ loop is delayed by 500ms? Once the ‘if’ condition is meet it should drop out of the timer/if condition.
thankyou very much…
I think this can resolve your problem…
EDIT: With this code you call the function by the first time. Function wait 500 ms and execute, in the final of the function, it checks if need to call another time (loop) and if needed it executes again. If you want to execute some code after that, you need to put it inside the ELSE of condition, because if you put another code below, it will be executed without wait. That’s because
setTimeoutandsetIntervalmakes the code asynchronous and continues to execute the code.