SitePoint JS Anthology
// object movement | |
function init() { | |
var obj = document.getElementById("move"); | |
obj.timerID = setInterval(function() { | |
moveObject(obj, 500, 0, 25); | |
}, 50); | |
} | |
function moveObject(target, destinationLeft, destinationTop, maxSpeed) { | |
var currentLeft = parseInt(target.style.left); | |
var currentTop = parseInt(target.style.top); | |
if(isNaN(currentLeft)) { currentLeft = 0; } | |
if(isNaN(currentTop)) { currentTop = 0; } | |
if (currentLeft < destinationLeft) { | |
currentLeft += maxSpeed; | |
if (currentLeft > destinationLeft) { currentLeft = destinationLeft; } | |
} else { | |
currentLeft -= maxSpeed; | |
if (currentLeft < destinationLeft) { currentLeft = destinationLeft; } | |
} | |
if (currentTop < destinationTop) { | |
currentTop += maxSpeed; | |
if (currentTop > destinationTop) { currentTop = destinationTop; } | |
} else { | |
currentTop -= maxSpeed; | |
if (currentTop < destinationTop) { currentTop = destinationTop; } | |
} | |
target.style.left = currentLeft + "px"; | |
target.style.top = currentTop + "px"; | |
if (currentLeft == destinationLeft && currentTop == destinationTop) { | |
clearInterval(target.timerID); | |
} | |
} | |
// simple clock fn | |
function clock() { | |
var div = document.createElement("div"); | |
div.id = "clock"; | |
document.body.appendChild(div); | |
(function showTime() { | |
var clock = document.getElementById("clock") | |
var today = new Date(); | |
clock.innerHTML = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds(); | |
setTimeout(showTime, 1000); | |
})(); | |
} | |
// setInterval(function() { | |
// var today = new Date(); | |
// console.log(today.getSeconds()); | |
// }, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment