我已经在我的组件上实现了加载控制器,它从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();
之间的时间.微秒
您可以在启动加载程序之前存储日期,也可以在关闭加载程序后再次存储日期。然后,您可以获得两个日期之间的差异。下面是一个示例代码。
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);
});
}
有关如何计算时差的更多详细信息,请参阅此答案。