-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsoleAnimation.js
More file actions
33 lines (28 loc) · 1.03 KB
/
consoleAnimation.js
File metadata and controls
33 lines (28 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
console.log("0".repeat(10))
// Adapted from https://stackoverflow.com/questions/34848505/how-to-make-a-loading-animation-in-console-application-written-in-javascript-or
/**
* Create and display a loader in the console.
*
* @param {string} [text=""] Text to display after loader
* @param {array.<string>} [chars=["⠙", "⠘", "⠰", "⠴", "⠤", "⠦", "⠆", "⠃", "⠋", "⠉"]]
* Array of characters representing loader steps
* @param {number} [delay=100] Delay in ms between loader steps
* @example
* let loader = loadingAnimation("Loading…");
*
* // Stop loader after 1 second
* setTimeout(() => clearInterval(loader), 1000);
* @returns {number} An interval that can be cleared to stop the animation
*/
function loadingAnimation(
text = "",
chars = ["⠙", "⠘", "⠰", "⠴", "⠤", "⠦", "⠆", "⠃", "⠋", "⠉"],
delay = 100
) {
let x = 0;
return setInterval(function() {
process.stdout.write("\r" + chars[x++] + " " + text);
x = x % chars.length;
}, delay);
}
loadingAnimation();