Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a loading animation in Console Application written in JavaScript or NodeJs?

How to make a loading animation in Console Application written in JavaScript or NodeJs?

Example animation or other animation.

1. --
2. \
3. |
4. /
5. --
like image 239
Юрий Светлов Avatar asked Jan 18 '16 06:01

Юрий Светлов


Video Answer


2 Answers

Not really possible in browser console. In Node.js:

var twirlTimer = (function() {
  var P = ["\\", "|", "/", "-"];
  var x = 0;
  return setInterval(function() {
    process.stdout.write("\r" + P[x++]);
    x &= 3;
  }, 250);
})();
like image 94
Amadan Avatar answered Sep 18 '22 12:09

Amadan


You can do it in the browser console as well:

var loading = (function() {
  var h = ['|', '/', '-', '\\'];
  var i = 0;

  return setInterval(() => {
    i = (i > 3) ? 0 : i;
    console.clear();
    console.log(h[i]);
    i++;
  }, 300);
})();

// clearInterval(loading) to stop it.
like image 27
saad-mb Avatar answered Sep 20 '22 12:09

saad-mb