在 Angular 9 中 'window.alert' 后无法退出函数
Unable to exit the function after 'window.alert' in Angular9
我正在尝试计算时差,如果时差大于 20 分钟,我想显示一个 alert message
然后退出。
我不打算遍历所有 rows
并想在找到具有 minutes < 20
.
的第一台主机后退出该功能
this.dataSource.filteredData.forEach(
async row => {
this.selection.select(row);
// const host = row.hostName.substring(0, row.hostName.indexOf('.'));
const host = 'abc';
const prevData = await this.myService.getData(host);
const timeDiff = Math.abs(new Date().getTime() - new Date(prevData[0].dateAt).getTime());
const minutes = Math.floor((timeDiff / 1000) / 60);
if (minutes < 20) {
window.alert('Please check after ' + (20 - minutes) + ' minutes.');
return false;
}
});
我正在尝试使用 return false
退出该函数,但它仍在遍历所有行。
没有 foreach
的更新代码:
const f = (async row => {
const host = 'abc';
const prevData = this.myService.getData(host);
const timeDiff = Math.abs(new Date().getTime() - new Date(prevData[0].dateAt).getTime());
const minutes = Math.floor((timeDiff / 1000) / 60);
return minutes;
});
(async () => {
for (let i = 0; i < this.dataSource.filteredData.length; i++) {
const mins = await f(this.dataSource.filteredData[i]);
if (mins < 200) {
window.alert('Please retry after ' + (200 - mins) + ' minutes.');
break;
}
}
})();
在上面的代码中,this.myService.getData(host)
返回 null
这里是 minimally-viable 控件编程方式的表示:
let rows = [1, 2, 3];
let f = (async row => {
return row === "2";
});
(async () => {
for (let i = 0; i < rows.length; i++) {
let result = await f(rows[i]);
console.log(result);
if (!result) {
break;
}
}
})();
我正在尝试计算时差,如果时差大于 20 分钟,我想显示一个 alert message
然后退出。
我不打算遍历所有 rows
并想在找到具有 minutes < 20
.
this.dataSource.filteredData.forEach(
async row => {
this.selection.select(row);
// const host = row.hostName.substring(0, row.hostName.indexOf('.'));
const host = 'abc';
const prevData = await this.myService.getData(host);
const timeDiff = Math.abs(new Date().getTime() - new Date(prevData[0].dateAt).getTime());
const minutes = Math.floor((timeDiff / 1000) / 60);
if (minutes < 20) {
window.alert('Please check after ' + (20 - minutes) + ' minutes.');
return false;
}
});
我正在尝试使用 return false
退出该函数,但它仍在遍历所有行。
没有 foreach
的更新代码:
const f = (async row => {
const host = 'abc';
const prevData = this.myService.getData(host);
const timeDiff = Math.abs(new Date().getTime() - new Date(prevData[0].dateAt).getTime());
const minutes = Math.floor((timeDiff / 1000) / 60);
return minutes;
});
(async () => {
for (let i = 0; i < this.dataSource.filteredData.length; i++) {
const mins = await f(this.dataSource.filteredData[i]);
if (mins < 200) {
window.alert('Please retry after ' + (200 - mins) + ' minutes.');
break;
}
}
})();
在上面的代码中,this.myService.getData(host)
返回 null
这里是 minimally-viable 控件编程方式的表示:
let rows = [1, 2, 3];
let f = (async row => {
return row === "2";
});
(async () => {
for (let i = 0; i < rows.length; i++) {
let result = await f(rows[i]);
console.log(result);
if (!result) {
break;
}
}
})();