Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 有没有办法计算异步函数被调用的次数?_Javascript - Fatal编程技术网

Javascript 有没有办法计算异步函数被调用的次数?

Javascript 有没有办法计算异步函数被调用的次数?,javascript,Javascript,我有一个从一系列数据链接上传文件的功能 我想做的是,如果数据链接数组包含3个文件 const testLinks=3和异步上传ImageData被激发三次我想控制台。在上传ImageData被激发三次后记录 我正在考虑进行计数,但每次启动uploadImageData时,我的所有测试都会重新开始计数 .ts async uploadImageData(formData: FormData) { const testLinks = 3; const uploadlink = answerA

我有一个从一系列数据链接上传文件的功能

我想做的是,如果数据链接数组包含3个文件

const testLinks=3
异步上传ImageData
被激发三次我想
控制台。在
上传ImageData
被激发三次后记录


我正在考虑进行计数,但每次启动
uploadImageData
时,我的所有测试都会重新开始计数

.ts

async uploadImageData(formData: FormData) {
  const testLinks = 3;
  const uploadlink = answerAtachmentUrl;
  const headers = headerLink;
  const loading = await this.loadingController.create({
    message: 'Uploading Photos and Files...',
  });
  await loading.present();
  this.httpClient.post<any>( uploadlink + this.userToken, formData, 
  { 'headers':headers }
    ).pipe(
      finalize(() => { loading.dismiss();})
    )
    .subscribe(res => {
      if (res['success']) {
        setTimeout(() => { this.DeleteAllFiles(); }, 5000);
        this.presentToastPhoto('Photo sync success.');
      } else {
        this.presentToastPhoto('Photo upload failed.');
        let respFail = JSON.stringify(res);
        console.log("respFail", respFail);
      }
    });
    // console.log fires once after count and const testLinks both equal 3
}
异步上传ImageData(formData:formData){
常数testLinks=3;
const uploadlink=answerAtachmentUrl;
const headers=headerLink;
const loading=等待this.loadingController.create({
消息:“正在上载照片和文件…”,
});
等待加载。当前();
this.httpClient.post(uploadlink+this.userToken,formData,
{'headers':headers}
).烟斗(
完成(()=>{loading.dismise();})
)
.订阅(res=>{
如果(res['success'])){
setTimeout(()=>{this.DeleteAllFiles();},5000);
这个。呈现给sthoto(“照片同步成功”);
}否则{
this.presentToastPhoto('照片上载失败');
让respFail=JSON.stringify(res);
console.log(“respFail”,respFail);
}
});
//console.log在count和const testLinks都等于3后激发一次
}
“我正在考虑进行计数,但每次启动uploadImageData时,我的所有测试都会重新开始计数。” 您没有发布您尝试的内容,但可能没有将计数器设置为全局变量。您的代码显然是一个更大项目的一部分,所以我做了这个小测试,以表明它可以与异步函数一起工作。如果不行,请告诉我

var计数=0;
异步函数uploadImageData(){
常数testLinks=3;
计数++;
控制台日志(计数);
如果(计数==3){
console.log('count=3');
}
}
上传ImageData();
上传ImageData();
上传ImageData();

上传ImageData()你能展示你所尝试的吗?只需使用一个标志变量。谢谢你,先生。我将var count保留在类中,而不是全局变量,并将订阅中的计数移动到每次运行时的计数。很好用