【问题标题】:Control flow in bacon.js, how to do something at a given timebacon.js 中的控制流,如何在给定时间做某事
【发布时间】:2013-10-03 06:20:52
【问题描述】:

所以我对函数式编程范式还很陌生,尤其是对 Bacon.js 和 FRP 很陌生。 我需要一些关于如何在 FRP 中概念化控制流的建议。我在事件流中有一个倒计时到零的计时器。当它达到零时,我想隐藏 HTML 计时器并停止事件流。

timer.coffee

# decrement function
dec = (x,y) ->
    x-y

# Create a timer counting down from 100 every 10th millisecond
timer = Bacon.interval(10, 1).scan(100, dec)

timer.onValue (e) ->
  # output the current timer value to the DOM
  $("#timer").text(e)

  # if the timer has reached 0, hide the DOM object 
  $("#timer").hide() if e is 0

timer.html

<body>
  <div id="timer"></div>
</body>

我真的应该像在onValue() 中那样使用 if/else 来检查一个值并调用一个函数吗?不知何故,感觉好像我做错了。当我对 eventStream 感到满意时,如何停止/关闭它?

【问题讨论】:

    标签: javascript coffeescript functional-programming bacon.js


    【解决方案1】:

    在定义流时,包括 takeWhile 以根据条件结束流。您可以使用 onEnd 在流结束时分配副作用。

    【讨论】:

    • 非常感谢!这当然成功了。我承认阅读文档时很草率。
    猜你喜欢
    • 2012-05-17
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 2011-07-08
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    相关资源
    最近更新 更多