如何在2个events- present()和dismiss()-IONIC 2之间计算LoadingController的时间

时间:2017-11-09 06:06:31

标签: angular ionic-framework ionic2

我已经在我的组件上实现了加载控制器,它带来了来自json api的数据,我希望实现计算在页面上显示数据的时间。

请找到我已实施OnInit

listproduct.ts的代码
export class ListproductPage implements OnInit{
  public list = [];
  loading;chkfromservice;
  constructor(private _listProduct : ListproductService,private _deleteProduct : DeleteProductService,
              public navCtrl: NavController,public navParams: NavParams,public loadingCtrl: LoadingController) {

                this.loading = this.loadingCtrl.create({
                  content: 'Please wait...'
                });

              }

      ngOnInit() {
        this.loading.present();
         this._listProduct.listProduct().subscribe(data => {
           this.list = data;
           console.log(this._listProduct.checkfromservice);
           this.chkfromservice = this._listProduct.checkfromservice;
          console.log(data[0].NAME);
          this.loading.dismiss();
          });
      }
  

请注意,我需要计算 this.loading.present();this.loading.dismiss();

之间的time.microseconds

1 个答案:

答案 0 :(得分:1)

您可以在启动装载机之前存储日期,并在解除装载机之后再次存储。然后,您可以获得两个日期之间的差异。这是一个示例代码..

  ngOnInit() {
    let start = new Date(); // Get the date just before presenting your loader
    this.loading.present();
     this._listProduct.listProduct().subscribe(data => {
       this.list = data;
       console.log(this._listProduct.checkfromservice);
       this.chkfromservice = this._listProduct.checkfromservice;
      console.log(data[0].NAME);
      this.loading.dismiss();
      let end = new Date(); // Get the date just after dismissing your loader

      let diff = (end.getTime() - start.getTime()); // Get the time difference in milliseconds
      console.log(diff);
      });
  }

有关如何计算时差的详细信息,请参阅this answer