Skip to content

Instantly share code, notes, and snippets.

@XiongLiding
Created April 23, 2014 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XiongLiding/11202423 to your computer and use it in GitHub Desktop.
Save XiongLiding/11202423 to your computer and use it in GitHub Desktop.
use samplingRate to limit the frequency of event trigger
# Define samplingRate
samplingRate = (interval) ->
mark = 0
->
now = Date.now()
return false if now - mark < interval
mark = now
# How to use
sampling = samplingRate 1000
setInterval ->
console.log 'called every 100ms'
if sampling()
console.log 'called every 1000ms'
, 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment