doc*_*sor 0 timedelay settimeout typescript angular
只能在这方面找到 JS 的东西。我只有重新加载页面元素的基本功能,我想将它们延迟 1-2 秒以等待 http 调用通过。我尝试了这个(从 rxjs 导入)但它根本不起作用
setTimeout(function () {
this.clearGroups();
this.prepareRow();
this.prepareGroups();
}, 2000);
Run Code Online (Sandbox Code Playgroud)
正如@VLAZ 指出的,您需要一个箭头函数(以“关闭”正确的 this 范围,例如:
setTimeout(() => {
this.clearGroups();
this.prepareRow();
this.prepareGroups();
}, 2000);
Run Code Online (Sandbox Code Playgroud)
不过,我建议您重新考虑您的解决方案,对于互联网连接非常差的用户,结果可能需要超过 2 秒“到达”,并且您是否想要惩罚具有快速连接的人等待 2 秒秒(更新出现)?
如果您的数据作为承诺到达,请考虑使用 async/await:
await getData();
this.clearGroups();
this.prepareRow();
this.prepareGroups();
Run Code Online (Sandbox Code Playgroud)
(请注意,这仅在从函数完成时才有效async,否则用作传统的 Promise .then(() => ...))
或作为可观察的:
getData().pipe(first()).subscribe(() => {
this.clearGroups();
this.prepareRow();
this.prepareGroups();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18452 次 |
| 最近记录: |