RxJS.Observable debounce有什么作用? [英] What does RxJS.Observable debounce do?

查看:197
本文介绍了RxJS.Observable debounce有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以用简单的英语解释 RxJS Observavle去抖功能吗?

Can anybody explain in plain English what RxJS Observavle 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 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);
    }
);

我用节点来说明这一点...假设您已经安装了节点,可以通过键入来运行它

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)

启动此节点程序后,您可以在控制台上键入值 - 如果您快速键入,则会忽略项目,如果间歇性地输入快速和慢速项目将在控制台上输入的间隙(在上面的示例中我有500毫秒)之后出现(下一个:)

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: ")

还有一些优秀的参考资料HREF = https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/debounce.md> https://github.com/Reactive-Extensions/RxJS/blob/ master / doc / api / core / operators / debounce.md

这篇关于RxJS.Observable debounce有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆