Attempting to utilize nested timeOut
with the same names, which functions similar to a loop, although not exactly. An example that I tried is:
let i = 1;
setTimeout(function run() {
func(i);
setTimeout(run, 100);
}, 100);
which was taken from this link.
It's important to note that using interval
and loop
is not possible as demonstrated in the link.
Below is my current code:
let i = 0;
let x = setTimeout(async function run() {
if(i == 2) {
// I intend to completely stop 'x' here
console.log(i)
clearTimeout(x);
}
try {
// Some code here e.g:
console.log(10)
} catch (err) {
// Some other code here e.g:
console.log(err)
}
i++;
x = setTimeout(run, 800);
}, 800);
And the output looks like this:
10
10
2
10
10
... //never stops
I also came across this link, but it doesn't address my specific issue.
Is there anyone who can suggest a way for me to completely stop x
?
Thanks in advance.