-4
我想循环while循环内的对象,但我无法得到结果..其实我是假设计数器的值是执行内部函数后递增而循环......但计数器的值是在函数执行之前得到提高......我没有得到为什么发生这种情况...虽然循环不等待,直到函数执行
这里是我的test.ts
export class HelloIonicPage {
_i: number = 0;
barcode(index, pickuplist) {
BarcodeScanner.scan()
.then((barcodeData) => {
if (barcodeData.cancelled) {
alert("User cancelled the action!");
return false;
}
this.bartxt = barcodeData.text;
if (this.bartxt != '') {
this.authservice.testFunction(this.bartxt)
.subscribe(data => {
if (data !== '') {
alert("Scanned successfully!");
alert(this._i); //here the value of i should be 0, but it showing 2
if (this._i == (this.totalorders - 1)) {
alert(this._i + " _index");
this.finalacceptvendor(index, pickuplist);
} else {
alert(this._i + " _index");
this.vendorbarcode(index, pickuplist);
}
} else {
this.vendorbarcode(index, pickuplist);
alert(Wrong QR Code);
}
});
}
});
}
vendorbarcode(index, pickuplist) {
this.totalorders = 2;
while (this._i < this.totalorders) {
alert(this._i + "_index"); //here the value of i = 0
this.barcode(index, pickuplist);
this._i = this._i + 1;
}
}
}
我在最后两天卡在这里...请帮助我解决这个问题
为什么不能得到结果? – mast3rd3mon
你能解释一下函数的用途吗?在会员级别存储计数器似乎是错误的方法。从我收集的内容看,您似乎有要扫描的项目列表,然后一旦扫描完成,就不需要再次扫描。我只想从列表中删除这些项目,而不是试图维护一个计数器。应该更简单。 – Brian
如果您不使vendorbarcode成为异步函数,那么您试图执行的操作是不可能的。 while循环不会等待异步操作,然后继续执行异步/等待操作。 –