RateLimit.coffee 379 B

1234567891011121314
  1. limits = {}
  2. call_after_interval = {}
  3. window.RateLimit = (interval, fn) ->
  4. if not limits[fn]
  5. call_after_interval[fn] = false
  6. fn() # First call is not delayed
  7. limits[fn] = setTimeout (->
  8. if call_after_interval[fn]
  9. fn()
  10. delete limits[fn]
  11. delete call_after_interval[fn]
  12. ), interval
  13. else # Called within iterval, delay the call
  14. call_after_interval[fn] = true