简体   繁体   中英

How to calculate time for LoadingController between 2 events- present() and dismiss() -IONIC 2

I have implemented loading controller on my component which brings data from json api , i wish to achieve the time calculated to bring the data on the page.

Please find the code i have implemented OnInit of 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();
          });
      }

Please note, i need to calculate the time.microseconds between this.loading.present(); and this.loading.dismiss();

You can store the date just before starting your loader and again just after dismissing your loader. You can then get the difference between the two dates. Here's a sample code..

  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);
      });
  }

Refer to this answer for more details on how the time difference is calculated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM