This is the code snippet of everything I think I need to describe this problem. I am trying to have a continuous action go off when the mousedown is activated, but am getting the error:
Uncaught ReferenceError: e is not defined
(anonymous function)
I’m pretty sure this error is stemming from the beginAction function where I have findClick(e) in quotes, somehow I don’t think e is getting passed correctly here:
function Cell(row, column) {
this.row = row;
this.column = column;
}
function foo(bar) {
//do stuff here
gCanvas.addEventListener("mousedown", beginAction, false);
document.addEventListener("mouseup", endAction, false);
}
function beginAction(e) {
findClick(e);
var findClick_timeout = setInterval("findClick(e)", 50);
}
function endAction(e) {
if (typeof(findClick_timeout) != "undefined"){ clearTimeout(findClick_timeout);}
}
function getCursorPosition(e) {
//finds the cell position here... works
var cell = new Cell(Math.floor(y/cellSize), Math.floor(x/cellSize));
return cell;
}
function findClick(e) {
var cell = getCursorPosition(e);
//do stuff with the cell!!!!!!
}
Timer strings are converted to functions in the global scope. That’s why you shouldn’t use them:
This way, that little anonymous function will have access to the “e” from the “beginAction” function it was created in. When you pass just a string, however, the runtime evaluates that in the global scope, and there’s no “e” out there.
In newer browsers, there’s a function called “bind” that you can use for this:
It’s just a tool for doing essentially what the anonymous function does.