Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
rAF-based random interval
const randomInterval = (() => {
const random = (min, max) => Math.random() * (max - min) + min;
return (callback, min, max) => {
const time = {
start: performance.now(),
total: random(min, max)
};
const tick = now => {
if (time.total <= now - time.start) {
time.start = now;
time.total = random(min, max);
callback();
}
requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
};
})();
randomInterval(() => console.log("hi"), 1000, 2000); // logs "hi" at a random interval between 1 and 2s
@bendc

This comment has been minimized.

Copy link
Owner Author

@bendc bendc commented Mar 9, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment