I am currently working on a project that requires the use of both setInterval
and setTimeout
. I am using setTimeout
with dynamic delays passed to it.
While elements are not timed out, I have implemented setInterval
to print out numbers.
Here is the code snippet:
const data = [
{ "entry": "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness." },
{ "entry": "No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful." },
{ "entry": "Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure." },
{ "entry": "This is a short one." },
{ "entry": "This is line two and it is long" },
{ "entry": "This is line two and it is long" },
{ "entry": "This is line three and it is also long" },
{ "entry": "This is line four and it is very very long" },
{ "entry": "This is line five and it is the longest line of all" },
{ "entry": "This is line six and it is longer than the previous line" },
{ "entry": "B" },
{ "entry": "Who built this country hand by hand, brick by brick. (Applause.) They all came here knowing that what makes somebody an American is not just blood or birth, but allegiance to our founding principles and the faith in the idea that anyone from anywhere can write the next great chapter of our story." }
];
// Calculate string length for each entry
const stringLength = [];
data.forEach(
(a, i) => {
stringLength.push(a.entry.length);
}
);
// Container to define outer delay for each setTimeout loop
const outerDelay = [];
// Predefine inner delay
const innerDelayEach = 10;
data.forEach(
(a, i, r) => {
(i === 0) ? outerDelay.push(0 * innerDelayEach): outerDelay.push(r[i - 1].entry.length * innerDelayEach);
}
);
let cntOuter = 0;
const elmnt = document.querySelectorAll('div');
const outerTerminator = elmnt.length - 1;
function show() {
const select = elmnt[cntOuter];
let cntInner = 0;
const inner = setInterval(
function() {
const text = select.textContent;
const innerTerminator = text.length - 1;
const val = text[cntInner];
console.log(cntOuter, cntInner, val);
cntInner++;
if (cntInner > innerTerminator) {
clearInterval(inner)
}
}, innerDelayEach
)
cntOuter++;
if (cntOuter > outerTerminator) return;
setTimeout(show, outerDelay[cntOuter])
};
show();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>...</div>
...
</body>
<script type="text/javascript"></script>
</html>
With the above setup, I expect the browser to execute the callbacks from setTimeout
at the specified delays, and I expect to see the characters printed by setInterval
without any overlap as long as each element is not timed out.
However, I am experiencing unexpected overlap in the output. Instead of cntOuter=1
executing till cntInner=263
, it goes to cntOuter=2 while cntInner=240
.
https://i.stack.imgur.com/rWKtR.png
There seems to be an issue causing this overlap, but I'm unable to identify the trigger.