Created
April 23, 2014 04:04
-
-
Save XiongLiding/11202423 to your computer and use it in GitHub Desktop.
use samplingRate to limit the frequency of event trigger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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