【问题标题】:continuously loop through values in an array using Baconjs使用 Baconjs 连续循环遍历数组中的值
【发布时间】:2015-12-01 19:19:55
【问题描述】:

我有一个字符串数组var strs = ['a','b','c'],我想使用Bacon.interval(2000) 将值连续返回到onValue

我能想到的最接近的方法是

var stream = Bacon.interval(2000);
var i = 0;
stream.onValue(function (v) {
    if (i >= strs.length) i=0;
    else i ++;
    strs[i];
})

我知道这不是一个非常被动的解决方案

【问题讨论】:

    标签: javascript frp bacon.js


    【解决方案1】:
    Bacon.repeatedly(2000, ['a', 'b', 'c'])
      .onValue(function(v) {
        console.log(v);
      });
    

    【讨论】:

    • OP 可能想要Bacon.repeatedly 而不是顺序
    • 谢谢!快速跟进,是否还有订阅者可以立即输出第一项而不是在前 2 秒后输出?
    • 你可以使用Bacon.repeatedly(2000, ['b', 'c']).toProperty('a'),它应该会产生这种行为。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 2020-10-06
    相关资源
    最近更新 更多