1function timeout() {
2 setTimeout(function () {
3 // Do Something Here
4 // Then recall the parent function to
5 // create a recursive loop.
6 timeout();
7 }, 1000);
8}
1// make sure to use "let" and not "var" if you want to capture
2// the value of the external variable in a closure
3
4 for (let i = 0; i < 5; i++) {
5 setTimeout(() => console.log(i), 0);
6 }
1var array = [1, 2, 3, 4, 5]for(var i = 0; i < array.length; i++) { setTimeout(() => { console.log(array[i]) }, 1000);} // i = 5
1var time = 1;
2
3var interval = setInterval(function() {
4 if (time <= 3) {
5 alert(time);
6 time++;
7 }
8 else {
9 clearInterval(interval);
10 }
11}, 5000);
12
1//you can leave the sleep constant
2const sleep = (milliseconds) => {
3 return new Promise(resolve => setTimeout(resolve, milliseconds))
4}
5
6const doSomething = async () => {
7 for (/*for loop statements here*/) {
8 //code before sleep goes here, just change the time below in milliseconds
9 await sleep(1000)
10 //code after sleep goes here
11 }
12 }
13doSomething();