DrakaSAN
DrakaSAN

Reputation: 7853

Using the console to print a changing variable in node.js

I have a parsing application in node.js.

There can be >100k lines to parse, so the app seems to "hang" for some times before printing it's done.

I could print "Parsing line X" each time, but if I do so, the console will just be overflown with text too fast to be useful.

What I would imitate is the same thing you see on OS loading or some console app on linux, where a value on the console is changing dynamically, a example would be wget on Debian, where a arrow is growing from left to right to simulate a download bar.

I d like to do something similar, but I don t even know the name of this.

Is it possible to achieve that in node.js? What is the name of this type of thing? (so I can search more info).

Upvotes: 0

Views: 307

Answers (2)

user934801
user934801

Reputation: 1139

Hi DrakaSAN i guess you are looking for a progress bar. An example can be found on https://github.com/visionmedia/node-progress

var ProgressBar = require('progress');

var bar = new ProgressBar(':bar', { total: 10 });
var timer = setInterval(function () {
  bar.tick();
  if (bar.complete) {
    console.log('\ncomplete\n');
    clearInterval(timer);
  }
}, 100);

Hope this is helpful to you.

Upvotes: 1

Chakravarthy S M
Chakravarthy S M

Reputation: 618

For the same effect in browser console, you can do like below

function call(i){

    setTimeout(function(){
      console.log(i);
    },i*100);
       setTimeout(function(){
      console.clear();
    },i*110);

}
for(var i=0;i<2000;i++){
    call(i);

}

but i don't know if the same can apply for console in node, can you try once, because i dnt have node setup for now in my system

Upvotes: 1

Related Questions