console
var box = document.getElementById('box'),
boxPos = 10,
limit = 300;
function draw() {
box.style.left = boxPos + 'px';
}
var boxVelocity = 0.08,
delta = 0;
function update(delta) {
boxPos += boxVelocity * delta;
if (boxPos >= limit || boxPos <= 0) boxVelocity = -boxVelocity;
}
var lastFrameTimeMs = 0,
maxFPS = 60;
var timestep = 1000 / 60;
function mainLoop(timestamp) {
if (timestamp < lastFrameTimeMs + (1000 / maxFPS)) {
requestAnimationFrame(mainLoop);
return;
}
delta += timestamp - lastFrameTimeMs;
lastFrameTimeMs = timestamp;
while (delta >= timestep) {
update(timestep);
delta -= timestep;
}
draw();
requestAnimationFrame(mainLoop);
}
requestAnimationFrame(mainLoop);
<div id="box"></div>
#box {
background-color: red;
height: 50px;
left: 150px;
position: absolute;
top: 10px;
width: 50px;
}