问题
Is it necessary to call clearTimeout()
inside a recursively invoked function in Coffeescript?
My concern is whether not calling clearTimeout()
will possibly cause some sort of memory leak over time if this function is run many many times per second. My thinking is the JS garbage collector handles this, but want to double check.
A contrived example from a websockets/socket.io implementation I'm working on:
socket.on 'dataReceived', => @_recursive_fn()
_recursive_fn: ->
@timer = setTimeout (=>
clearTimeout(@timer) # is this necessary?
@_recursive_fn() if some_condition == true
), 30
回答1:
No, setTimeout
schedules a one-off event. Once the event has occurred, clearTimeout
with that handle is a no-op.
来源:https://stackoverflow.com/questions/26522783/is-it-necessary-to-cleartimeout-inside-a-recursively-invoked-timer