Created
April 26, 2020 18:39
-
-
Save lgessler/e6a2663e21c91da9b2198fc297d2a3c7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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