Skip to content

Instantly share code, notes, and snippets.

@uiur
Created March 24, 2014 08:08
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 uiur/9736118 to your computer and use it in GitHub Desktop.
Save uiur/9736118 to your computer and use it in GitHub Desktop.
app.service 'timeoutIfLast', [
'$q'
'$interval'
($q, $interval) ->
# delayの間, watchFnがtrueのまま持続したときに実行されるpromiseを返す
#
# @param {Function} watchFn
# @param {Number} delay ms
# @param {Number} [watchInterval] ms
# @return {Promise}
(watchFn, delay, watchInterval=100) ->
deferred = $q.defer()
currentDelay = 0
timer = $interval(->
if watchFn()
currentDelay += watchInterval
if currentDelay >= delay
deferred.resolve()
$interval.cancel(timer)
else
deferred.reject()
$interval.cancel(timer)
, watchInterval)
deferred.promise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment