Skip to content

Instantly share code, notes, and snippets.

@cfleschhut
Created May 17, 2011 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfleschhut/977616 to your computer and use it in GitHub Desktop.
Save cfleschhut/977616 to your computer and use it in GitHub Desktop.
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