| 123 | } |
| 124 | |
| 125 | function Example5(onComplete){ |
| 126 | // EXAMPLE 5 --------------------------------------------- |
| 127 | console.log('\nExample 5 - Custom Payload'); |
| 128 | // create new progress bar |
| 129 | const b1 = new _progress.Bar({ |
| 130 | format: 'progress [{bar}] {percentage}% | ETA: {eta}s | {value}/{total} | Speed: {speed}' |
| 131 | }); |
| 132 | |
| 133 | // initialize the bar - defining payload token "speed" with the default value "N/A" |
| 134 | b1.start(200, 0, { |
| 135 | speed: "N/A" |
| 136 | }); |
| 137 | |
| 138 | // the bar value - will be linear incremented |
| 139 | let value = 0; |
| 140 | |
| 141 | const speedData = []; |
| 142 | |
| 143 | // 20ms update rate |
| 144 | let timer = setInterval(function(){ |
| 145 | // increment value |
| 146 | value++; |
| 147 | |
| 148 | // example speed data |
| 149 | speedData.push(Math.random()*2+5); |
| 150 | const currentSpeedData = speedData.splice(-10); |
| 151 | |
| 152 | // update the bar value |
| 153 | b1.update(value, { |
| 154 | speed: (currentSpeedData.reduce(function(a, b) { return a + b; }, 0) / currentSpeedData.length).toFixed(2) + "mb/s" |
| 155 | }); |
| 156 | |
| 157 | // set limit |
| 158 | if (value >= b1.getTotal()){ |
| 159 | // stop timer |
| 160 | clearInterval(timer); |
| 161 | |
| 162 | b1.stop(); |
| 163 | |
| 164 | // run complete callback |
| 165 | onComplete.apply(this); |
| 166 | } |
| 167 | }, 20); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | function Example6(onComplete){ |