慕容森
2023-08-24 18:01:27
我有從數(shù)據(jù)鏈接數(shù)組上傳文件的功能我想做的是如果數(shù)據(jù)鏈接數(shù)組包含 3 個(gè)文件const testLinks = 3;并且async uploadImageData被解雇了三次我想在被解雇了三次console.log之后。uploadImageData我正在考慮進(jìn)行計(jì)數(shù),但我所有的測(cè)試都會(huì)在每次被觸發(fā)時(shí)重新開始計(jì)數(shù)uploadImageData。.tsasync 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}
1 回答

侃侃爾雅
TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超16個(gè)贊
“我正在考慮進(jìn)行計(jì)數(shù),但我所有的測(cè)試都從每次 uploadImageData 被觸發(fā)時(shí)開始計(jì)數(shù)?!?您沒有發(fā)布您嘗試過(guò)的內(nèi)容,但您可能沒有將計(jì)數(shù)器設(shè)為全局變量。您的代碼顯然是一個(gè)更大項(xiàng)目的一部分,因此我只是做了這個(gè)小測(cè)試來(lái)表明它可以與異步函數(shù)一起使用。如果不起作用,請(qǐng)告訴我。
var count=0;
async function uploadImageData() {
const testLinks = 3;
count++;
console.log(count);
if (count === 3){
console.log('count = 3');
}
}
uploadImageData();
uploadImageData();
uploadImageData();
uploadImageData();
添加回答
舉報(bào)
0/150
提交
取消