RxJS.Observable 去抖动有什么作用? [英] What does RxJS.Observable debounce do?
问题描述
谁能用简单的英语解释一下什么RxJS Observable 去抖动功能 有吗?
Can anybody explain in plain English what RxJS Observable debounce function does?
我想它会根据参数偶尔发出一个事件,但我下面的代码没有按预期工作.
I imagine it emits an event once in a while depending on the parameters, but my code below doesn't work as I expected.
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);
});
和JsBin 版本.
我希望这段代码每秒打印一次点击,无论我点击多快.相反,它以我认为随机的间隔打印点击.
I expected that this code would print one click once per second, no matter how fast I am clicking. Instead it prints the click at what I think are random intervals.
推荐答案
Debounce 将在指定的时间间隔过去后发出一个值,而不会发出另一个值.
Debounce will emit a value after a specified time interval has passed without another value being emitted.
使用以下简单的图表可能会提供更大的帮助:
Using simple diagrams the following may provide greater help:
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)被忽略.
The intermediate items (in this case, 2,3,4) are ignored.
示例如下:
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 来说明这一点...假设你已经安装了 node,你可以通过输入
I used node to illustrate this... assuming you have node installed, you can run it by typing
$node myfile.js (where the aforementioned code is in myfile.js)
一旦这个节点程序启动,你就可以在控制台输入值——如果你输入快速项目将被忽略,如果输入间歇性快速和慢速项目将在输入间隔后出现(在上面的例子中我有 500ms)在控制台(下一个:")
Once this node program is started you can type values at the console -- if you type quickly items are ignored, and if type intermittently fast and slow items will appear after a gap in typing (in the example above I have 500ms) at the console ("Next: ")
在 https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/debounce.md
这篇关于RxJS.Observable 去抖动有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!