Skip to content

Instantly share code, notes, and snippets.

@lgessler
Created April 26, 2020 18:39
Show Gist options
  • Save lgessler/e6a2663e21c91da9b2198fc297d2a3c7 to your computer and use it in GitHub Desktop.
Save lgessler/e6a2663e21c91da9b2198fc297d2a3c7 to your computer and use it in GitHub Desktop.
// print 10 x's per second
function printXs(totalMs) {
let begin = null;
let printed = 0;
function callback(tick) {
// note begin time
if (begin === null) {
begin = tick;
}
// make sure we're still in the animation interval
let lapsed = tick - begin;
if (lapsed <= totalMs) {
// calculate the number of x's to print now and print them
let xs = "x".repeat(Math.floor(lapsed / 100) - printed);
if (xs.length > 0) {
printed += xs.length;
console.log(xs);
}
// recur
requestAnimationFrame(callback);
}
// print any remaining after our period has expired
else if (printed < Math.floor(totalMs / 100)) {
let xs = "x".repeat(Math.floor(totalMs / 100) - printed);
console.log(xs);
}
}
requestAnimationFrame(callback);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment