1

所以我对函数式编程范式相当陌生,尤其是对 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>

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

4

1 回答 1

4

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

于 2013-10-04T05:51:48.870 回答