Created
May 17, 2011 23:03
-
-
Save cfleschhut/977616 to your computer and use it in GitHub Desktop.
SitePoint JS Anthology
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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