Created
December 10, 2019 12:09
-
-
Save tomshaw/38251cc6c5161e7e35eedc5bb501e6d5 to your computer and use it in GitHub Desktop.
My answer to Facebook JavaScript interview question on dev.to.
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
// https://dev.to/coderbyte/a-javascript-interview-question-asked-at-facebook-27po | |
var el = document.getElementById("myDiv"); | |
function animate(el, totalTime, distance, startTime, position) { | |
if (position >= distance) return; | |
var expired = (new Date() - startTime) / 1000; | |
position = expired * distance / totalTime * 1000; | |
el.style.left = position + 'px'; | |
requestAnimationFrame(function() { | |
animate(el, totalTime, distance, startTime, position); | |
}); | |
} | |
animate(el, 2000, 300, new Date(), 50); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment