JavaScript variables are scoped to the function in which they are defined, which means that up_timer
is local to the function passed to mousedown
and cannot be accessed outside of it.
Declaring the variable in the parent function makes it available throughout:
$(document).ready(function() {
var up_timer;
$("#pad").mousedown(function() {
$(this).css("background-color", "lightgrey");
var timer = 1;
up_timer = setInterval(function() {
timer = timer + 1;
$("#pad").html(timer);
}, 1);
}).mouseup(function() {
$(this).css("background-color", "grey");
clearInterval(up_timer);
});
});