Created
September 30, 2014 09:19
-
-
Save zapthedingbat/c0e5b754941d391da8b1 to your computer and use it in GitHub Desktop.
Calling the returned function signals to run the specified function or, if the function is already executing, flag that it should run once more.
This file contains 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
function onceMore(f) { | |
var isWorking = false, | |
isWaiting = false; | |
function execute() { | |
isWaiting = false; | |
isWorking = true; | |
f().then(complete); | |
} | |
function complete() { | |
if (isWaiting) { | |
execute(); | |
} else { | |
isWorking = false; | |
} | |
} | |
function queue() { | |
if (isWorking) { | |
isWaiting = true; | |
} else { | |
execute(); | |
} | |
}; | |
return queue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Usage