计算百分比的剩余百分比

时间:2021-07-11 02:14:02

标签: javascript math

我正在进行几次下载,我加载第一个模块,百分比固定为 70%,然后加载其他几个模块我在哪里知道下载第一个模块后的模块长度,所以我尝试填写 30% 以完成 100% 的进度。

export default class ModuleLoader {
  private percent;
  private first_module_loaded;

  constructor() {
    this.percent = 0;
    this.first_module_loaded = false;
    this.loadFirstModule();
  }

  public updatePercentage(percent) {
    percent = Math.trunc(percent);

    if (this.first_module_loaded) {
      percent = Math.trunc((((percent / 100) * 0.3) * 100));; // here is
      this.percent += percent;
    } else if (!this.first_module_loaded && percent >= 70) this.percent = 70;
    else this.percent += percent;
  }

  public loadFirstModule() {
    //...load the first module
    //this.updatePercentage((bytesLoaded / bytesTotal) * 100)

    //after complete the percentage is fixed to 70%, and load other modules with the modules length retrieved from the first module
    this.first_module_loaded = true;

    this.loadOthersModules(modules_length);
  }

  public loadOthersModules(modules_length) {
    //...load others modules
    //this.updatePercentage(((bytesLoaded / bytesTotal) * 100) / modules_length)
  }
}

我试过 Math.trunc((((percent / 100) * 0.3) * 100)) 但最后百分比总是大于 100

0 个答案:

没有答案