Skip to content

Instantly share code, notes, and snippets.

@ajhekman
Created January 10, 2014 21:26
Show Gist options
  • Select an option

  • Save ajhekman/8362952 to your computer and use it in GitHub Desktop.

Select an option

Save ajhekman/8362952 to your computer and use it in GitHub Desktop.
perform action at specific time
setSpecific = (callback, hours=0, minutes=0, seconds=0, options={tickRate:10000, repeat:true}) ->
start = new Date()
target = new Date()
#add a day to target if we're already past the time
target.setDate start.getDate() + 1 if start.getHours() > hours or (start.getHours() is hours and start.getMinutes() > minutes) or start.getHours() is hours and start.getMinutes() is minutes and start.getSeconds() >= seconds
target.setHours hours
target.setMinutes minutes
target.setSeconds seconds
console.log "set refresh for #{target}"
intervalID = setInterval ->
unless options.repeat then clearInterval intervalID
if new Date() > target
callback()
, options.tickRate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment