Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8555461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:07:40+00:00 2026-06-11T15:07:40+00:00

I’m trying to access the variable myvar from outside like so: $(document).mousemove(function(e){ var myvar

  • 0

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!)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:07:41+00:00Added an answer on June 11, 2026 at 3:07 pm

    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:

    var myVar; // the global variable
    
    // the function that will be caled when myVar has been changed
    var myVarChangedHandler = function() {
        console.log('myVar variable has been changed: ' + myVar);
    }
    
    // bind the event to the above event handler
    $('body').bind('MyVarChangedEvent', myVarChangedHandler);
    
    // instal mouse move event handler on document
    $(document).mousemove(function(e){
        myVar = winHeight() + scrollY() - e.pageY;
        $('body').trigger('MyVarChangedEvent');
    }); 
    

    UPDATE

    Removed the var keyword form the movemove event handler.

    The code that depends on myVar should be put in the myVarChangedHandler function.

    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)

    var myvar;
    $(document).mousemove(function(e){
        myvar = winHeight() + scrollY() - e.pageY;
    }); 
    console.log(myvar);
    

    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 myvar everytime the mouse moves)
    You have at your disposal a repository (var myvar) and 2 developers:

    • developer John (function(e) { myVar = ... };)
    • developer Ken (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

    var myvar;
    

    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

    $(document).mousemove(function(e){
        myvar = winHeight() + scrollY() - e.pageY;
    });         
    

    09:11 You tell Ken:
    Ken, please test the code in the repository right now and go home after you
    have finished testing

    console.log(myvar);
    

    (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 myvar but you haven’t even moved the mouse
    so, of course, the value is undefined

    To 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:

    John, please do this tasks and everytime you complete each task
    uploaded it to the repository and tell Ken to test the code.
    Go home after you have finished
    

    09:11 You tell Ken:

    Ken, each time John tells you that he has completed a task,
    fetch the code from the repository and test it.
    Go home after he has finished
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.