I’m trying to access the variable myvar from outside like so:
$(document).mousemove(function(e){
var myvar = winHeight() + scrollY() - e.pageY;
});
console.log(myvar);
But Chrome’s javascript console keeps saying “Uncaught ReferenceError: myvar is not defined all-products:203
(anonymous function)”
What am I doing wrong? How do I access this variable outside this function?
EDIT: I’ve realised that what I was attempting to do isn’t realisable. I have since changed my strategy completely and the code works fine now. (Thank you James and especially Vlad for your help!)
I think this is a job for javascript events. Basically you have a global variable which will be updated on mouse move. After you update the variable you have to let other components know that the variable is ready for processing.
The code:
UPDATE
Removed the
varkeyword form the movemove event handler.The code that depends on myVar should be put in the
myVarChangedHandlerfunction.I’ll try to explain the best i can your code, the flaw in it
and how you should be solving the problem using an analogy.
Let’s take the following code (global variable corrected)
Let’s say you, the programmer, are a team leader in a web development department
in some unnamed company and you have a list of tasks to be done in a day of work (
the tasks in our analogy is to update
myvareverytime the mouse moves)You have at your disposal a repository (
var myvar) and 2 developers:function(e) { myVar = ... };)console.log(myvar))09:00 You come to the office in the morning (user opens the page)
09:05 You power up the server / repository
09:10 You tell John:
John, please do this tasks and everytime you complete each task
uploaded it to the repository, and go home after your hours are finished
09:11 You tell Ken:
Ken, please test the code in the repository right now and go home after you
have finished testing
(at this time, Ken sees that the repository is empty and goes home – that’s because
John didn’t had time to solve even on task in a minute so Ken has nothing to test)
09:12 You go home
At 09:12 there’s only John at the office doing the tasks, You and Ken have left home
because you don’t have anything else to do.
This also happens to your code. You output the value of
myvarbut you haven’t even moved the mouseso, of course, the value is
undefinedTo solve this we add some modifications:
09:00 You come to the office in the morning
09:05 You power up the server (repository)
09:10 You tell John:
09:11 You tell Ken:
09:12 You go home
at 09:12 both John and Ken are at the office doing their job.
In the above case ken is
myVarChangedHandler = function() {...};When John tells Ken that he completed the task an actual event occurs (the telling),
when Ken acknowleges John signal he starts testing (Ken is the event handler)
This is how event driven architecture in javascript works.
I would advise you to ditch jquery, mootools, etc… and start learning the core concepts
and the basics. Reinvent the wheel a few times then go back to jquery.
I hope i explained enough for you to understand.