为什么油门不工作?
Why isn't throttle working?
我正在使用 rx
创建一个自动完成框。这是我的代码:
let input = document.getElementById('input');
var keypresses = Observable.fromEvent(input, 'keyup');
var searchResults = keypresses.
throttle(100).
map(key => {
return getResults(input.value);
}).
switchLatest();
let results = document.getElementById('results');
searchResults.forEach(resultSet => results.value = resultSet);
这是对 wikipedia
的 http
调用。如果我在 keyup
之间等待 100 ms
,我希望 throttle
只发送数据,但它发送一个 http
请求,每个 keyup
.
您可以在 image
中看到它正在对每个 keypress
进行单独调用。
我做错了什么?如何让 throttle
执行我期望的操作?
您要找的是Rx.Observable.prototype.debounce
吗?比照。 https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/debounce.md。
它说:Ignores values from an observable sequence which are followed by another value within a computed debounced duration.
其实debounce
和throttle
很容易混淆,希望是你要找的。在 Rxjs 2 中,语义实际上是相反的,所以请检查您使用的版本和相应的文档。
实际上你也可以在这里查看 debounce
使用 Rxjs 自动补全的例子:https://github.com/Reactive-Extensions/RxJS/blob/master/examples/autocomplete/autocomplete.js
我正在使用 rx
创建一个自动完成框。这是我的代码:
let input = document.getElementById('input');
var keypresses = Observable.fromEvent(input, 'keyup');
var searchResults = keypresses.
throttle(100).
map(key => {
return getResults(input.value);
}).
switchLatest();
let results = document.getElementById('results');
searchResults.forEach(resultSet => results.value = resultSet);
这是对 wikipedia
的 http
调用。如果我在 keyup
之间等待 100 ms
,我希望 throttle
只发送数据,但它发送一个 http
请求,每个 keyup
.
您可以在 image
中看到它正在对每个 keypress
进行单独调用。
我做错了什么?如何让 throttle
执行我期望的操作?
您要找的是Rx.Observable.prototype.debounce
吗?比照。 https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/debounce.md。
它说:Ignores values from an observable sequence which are followed by another value within a computed debounced duration.
其实debounce
和throttle
很容易混淆,希望是你要找的。在 Rxjs 2 中,语义实际上是相反的,所以请检查您使用的版本和相应的文档。
实际上你也可以在这里查看 debounce
使用 Rxjs 自动补全的例子:https://github.com/Reactive-Extensions/RxJS/blob/master/examples/autocomplete/autocomplete.js