Is it necessary to clearTimeout inside a recursively invoked timer?

眉间皱痕 提交于 2019-12-31 04:29:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!