Skip to content

Commit b2533ad

Browse files
author
Marco Brack
committed
Don't render if last render was less than renderThrottle ms ago
This approach works with asynch and synch usecases. Fixes issue #170.
1 parent 10c2233 commit b2533ad

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

lib/node-progress.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ function ProgressBar(fmt, options) {
6666
head : options.head || (options.complete || '=')
6767
};
6868
this.renderThrottle = options.renderThrottle !== 0 ? (options.renderThrottle || 16) : 0;
69+
this.lastRender = -Infinity;
6970
this.callback = options.callback || function () {};
7071
this.tokens = {};
7172
this.lastDraw = '';
@@ -92,7 +93,7 @@ ProgressBar.prototype.tick = function(len, tokens){
9293

9394
this.curr += len
9495

95-
// schedule render
96+
// try to render
9697
this.render();
9798

9899
// progress complete
@@ -118,6 +119,14 @@ ProgressBar.prototype.render = function (tokens) {
118119

119120
if (!this.stream.isTTY) return;
120121

122+
var now = Date.now();
123+
var delta = now - this.lastRender;
124+
if (delta < this.renderThrottle) {
125+
return;
126+
} else {
127+
this.lastRender = now;
128+
}
129+
121130
var ratio = this.curr / this.total;
122131
ratio = Math.min(Math.max(ratio, 0), 1);
123132

0 commit comments

Comments
 (0)