I have a requirement where I need to add delay in between messages.
Imagine the messages as chat messages.var object = [1, 2, 3, 4, 5];
these are the five messages and number 3 is the delay message.
When I reach 3 I need to show typing.. for lets say 2s. and after that 4 and 5 should show.
Here is what I try:
var object = [1, 2, 3, 4, 5];
for (i = 0; i < object.length; i++) {
if (object[i] == 3) {
$('div').append('typing');
sleep(2);
}
console.log(object[i]);
}
function sleep(seconds) {
var waitUntil = new Date().getTime() + seconds * 1000;
while (new Date().getTime() < waitUntil) true;
}
My current problem is the div
is appending after 2 second not before the delay start.
Why this happen? What is the solution?
Please help Thanks in advance.