1 javascript rxjs typescript scadalts angular
我有一个 http 请求,如果用户在输入中输入至少 4 个字符并在每次更改内容时触发(添加/删除字母),就会触发该请求。我想添加一个超时,如果用户开始输入字符,该函数将等待 1 秒直到它触发请求,以避免用户快速输入时出现大量请求。我的尝试:
if (this.pointName.length >= 3) {
let timer = function() {
this.http.get(`./points.json`)
.subscribe(res => {
this.pointsArray = res.json();
});
};
clearTimeout(timer);
setTimeout(timer,1000);
Run Code Online (Sandbox Code Playgroud)
我的想法是清除每个keyup事件的超时并再次设置它。但不幸的是,它给了我一个错误,即“() => void”类型的参数不能分配给“数字”类型的参数。
有没有办法更有效地做到这一点?也许使用 RxJS?无论如何,我正在寻找一个可行的解决方案。先感谢您。
HTML
<input type="text" id="searchInput" placeholder="Point name"(keyup)="getPoints()">
Run Code Online (Sandbox Code Playgroud)
为什么不使用 debounceTime(500) 而不是 setTimeout。
https://www.learnrxjs.io/operators/filtering/debouncetime.html
| 归档时间: |
|
| 查看次数: |
7121 次 |
| 最近记录: |