let p1 = document.getElementById("p1");; p1.style.left="100px"; let speed = 1; function moveText(){ if (parseInt(p1.style.left) < 0 || parseInt(p1.style.left) > (window.innerWidth - p1.offsetWidth)){ speed = -speed; } p1.style.left = (parseInt(p1.style.left) + speed) + "px"; } window.setInterval(moveText,10);