0

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

user2828442
  • 2,415
  • 7
  • 57
  • 105

1 Answers1

1

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.

Aamir Khan
  • 2,945
  • 2
  • 25
  • 32
  • perfect thank you , please vote for my question if i presented it nicely and can you further guide me , how can i calculate in kb the data i am trying to fetch from json , so i can corelate the time with amount of data – user2828442 Nov 09 '17 at 06:19
  • You can use the Content-Length header field of the response object to find out the number of bytes in the response body and convert those to kilobytes using the appropriate formula. If my answer has answered your `original question`, please mark it as accepted. – Aamir Khan Nov 09 '17 at 06:44