Skip to content

Instantly share code, notes, and snippets.

@hongz1
Forked from AndersDJohnson/setIntervalSynchronous.js
Last active August 26, 2015 12:17
Show Gist options
  • Save hongz1/3fad28950254e066a687 to your computer and use it in GitHub Desktop.
Save hongz1/3fad28950254e066a687 to your computer and use it in GitHub Desktop.
A synchronous version of setInterval (functional form). Waits for the interval function to finish before starting the timeout to the next call.
var setIntervalSynchronous = function (func, delay) {
var intervalFunction, timeoutId, clear;
// Call to clear the interval.
clear = function () {
clearTimeout(timeoutId);
};
intervalFunction = function () {
func();
timeoutId = setTimeout(intervalFunction, delay);
}
// Delay start.
timeoutId = setTimeout(intervalFunction, delay);
// You should capture the returned function for clearing.
return clear;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment