6 examples of 'timed loop' in JavaScript

Every line of 'timed loop' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
16export function loop(interval: number, callback: (...args: any[]) => any) {
17 const timer = setInterval(callback, interval)
18 return () => clearInterval(timer)
19}
11(function theLoop (i) {
12 setTimeout(function () {
13 console.log("Bam!");
14 if (--i) { // If i > 0, keep going
15 theLoop(i); // Call the loop again, and pass it the current value of i
16 }
17 }, 1000);
18 console.log("End of the story");
19})(5);
30async function loop() {
31 while(true) {
32 await checkCSVData();
33 sleep.sleep(600);
34 }
35}
15function loop() {
16 store.delta = Date.now() - start;
17 store.callHandler = store.delta >= delay ? fn.call() : request(loop);
18}
42function run() {
43 let task = highestPriorityTasks.shift();
44 if (!task) {
45 task = highPriorityTasks.shift();
46 }
47 if (!task) {
48 task = middlePriorityTasks.shift();
49 }
50 if (!task) {
51 task = lowPriorityTasks.shift();
52 }
53 if (!task) {
54 task = lowestPriorityTasks.shift();
55 }
56
57 if (task) {
58 console.time(task.name);
59 task.callback();
60 console.timeEnd(task.name);
61
62 timeout = setTimeout(run);
63 } else {
64 timeout = undefined;
65 }
66}
51function loop() {
52 if (runningAnimators > 0) {
53 // If there is at least one animator,
54 // and if the browser provides animation frames,
55 // schedule this function to be called again in the next frame.
56 if (requestAnimationFrame) {
57 requestAnimationFrame(loop);
58 }
59
60 // Step all animators. We iterate over a copy of the animator list
61 // in case the step() method removes an animator from the list.
62 animatorList.forEach(animator => {
63 if (animator.running) {
64 animator.step();
65 }
66 });
67 }
68 else if (!requestAnimationFrame) {
69 // If all animators have been removed,
70 // and if this function is called periodically
71 // by setInterval(), disable the periodic calling.
72 window.clearInterval(timer);
73 }
74}

Related snippets