谁能用简单的英语解释一下 RxJS Observable debounce function做?
我想它会根据参数不时发出一个事件,但是我下面的代码没有按我的预期工作。
var x$ = Rx.Observable.fromEvent(window, 'click')
.map(function(e) {return {x:e.x, y:e.y};})
.debounce(1000)
.subscribe(function(el) {
console.log(el);
});
和 the JsBin version .
我希望这段代码每秒打印一次点击,无论我点击多快。相反,它会以我认为的随机间隔打印点击。
最佳答案
Debounce 将在指定的时间间隔过后发出一个值,而不会发出另一个值。
使用简单的图表可以提供更大的帮助:
Stream 1 | ---1-------2-3-4-5---------6----
after debounce, the emitted stream looks like as follows:
Stream 2 | ------1-------------5---------6-
中间项(在本例中为 2、3、4)被忽略。
下面举例说明:
var Rx = require('rx-node');
var source = Rx.fromStream(process.stdin).debounce(500);
var subscription = source.subscribe(
function (x) {
console.log('Next: %s', x);
}
);
我用节点来说明这一点......假设你已经安装了节点,你可以通过键入来运行它
$node myfile.js (where the aforementioned code is in myfile.js)
启动此节点程序后,您可以在控制台输入值——如果您快速输入项目将被忽略,并且如果在控制台输入间隙(在上面的示例中我有 500 毫秒)后间歇性地输入快速和慢速项目(“下一个: ”)
https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/debounce.md 上还有一些优秀的引用资料
关于javascript - RxJS.Observable debounce 有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30840247/