const element = document.getElementById('square');
let startTime,
distance = 100,
duration = 2000;
function slideRight(timestamp, element, distance, duration) {
timestamp = timestamp || (new Date()).getTime();
const runtime = timestamp - startTime;
const progress = runtime / duration;
const nextPosition = (progress * distance).toFixed(2);
element.style.transform = `translateX(${nextPosition}px)`;
if (runtime < duration) {
requestAnimationFrame((timestamp) => {
slideRight(timestamp, element, distance, duration)
})
} else {
element.style.transform = `translateX(0px)`;
requestAnimationFrame((timestamp) => {
startTime = timestamp || (new Date()).getTime();
slideRight(timestamp, element, distance, duration);
})
}
}
requestAnimationFrame((timestamp) => {
startTime = timestamp || (new Date()).getTime();
slideRight(timestamp, element, distance, duration);
})