My following code will not function, unless I place it all
$ (window).load(function(){
// within here
}
How can I get my code to run without requiring the above function?
Thanks!
My code:
// Player controls
var timer = null;
$ ('#left').mousedown(function() {
moveLeft(); // Do it now
timer = setInterval(moveLeft, 5); // Then every 100 ms
}).mouseup(function() {
clearInterval(timer); // And stop it
});
function moveLeft() {
var nextpos = parseInt($ ('#player').css('left')) - 5;
if (nextpos > 0) {
$ ('#player').css('left', nextpos + 'px');
}
}
$ ('#right').mousedown(function() {
moveRight(); // Do it now
timer = setInterval(moveRight, 5); // Then every 100 ms
}).mouseup(function() {
clearInterval(timer); // And stop it
});
function moveRight() {
var nextpos = parseInt($ ('#player').css('left')) + 5;
if (nextpos < PLAYGROUND_WIDTH - 100) {
$ ("#player").css("left", ""+nextpos+"px");
}
}
// Timer
$ (function(){
var timercount = 30;
countdown = setInterval(function(){
$ ("#timer").html(timercount);
if (timercount <= 0) {
$ ("#timer").html("TIMES UP");
}
timercount--;
}, 1000);
});