为什么油门不工作?

时间:2015-12-01 21:03:58

标签: javascript rxjs

我正在使用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);

这是对http的{​​{1}}来电。如果我在wikipedia之间等待throttle,我希望100 ms仅发送数据,但它会向每个keyup发送http个请求。< / p>

您可以在keyup中看到它与每个image进行单独通话。

chrome console

我做错了什么?如何让keypress做我期望的事情?

1 个答案:

答案 0 :(得分:4)

您正在寻找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.

真相debouncethrottle很容易混淆,希望它正是您所寻找的。在Rxjs 2中,语义实际上是相反的,因此请检查您使用的版本和相应的文档。

实际上,您也可以在此处查看debounce与Rxjs进行自动完成的示例:https://github.com/Reactive-Extensions/RxJS/blob/master/examples/autocomplete/autocomplete.js

相关问题