?
Solved

javascript global variable not updating

Posted on 2014-09-28
3
Medium Priority
?
345 Views
Last Modified: 2014-09-29
hello experts

I have
var myGlobalvar;

var addRow= function(view){
myGlobalvar = null;
...
...

myGlobalvar = view;
}

Open in new window

When addRow function is invoked first time it works fine. i.e i am able to see 'view' variable value assigned to
myGlobalvar. (myGlobalvar = view;)

But when addRow function is invoked second time, myGlobalvar still has reference to old value of 'view' variable , i.e myGlobalvar is not getting updated with new value of view.

Any clue what i am doing wrong?

Thanks.
0
Comment
Question by:royjayd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 1600 total points
ID: 40349159
replace :
var myGlobalvar;
by :
window.myGlobalvar;
0
 
LVL 43

Expert Comment

by:Rob
ID: 40349160
Impossible to say from the scope of code you've provided as it looks ok to me.

Try debugging with your browser's dev tools (F12 or Ctrl+Shift+i) and set a breakpoint inside that function.

Inspect the locals on the first and subsequent calls to the function to make sure that the "view" argument is in fact a new value as well as how the global variable is being updated.
0
 
LVL 43

Assisted Solution

by:Rob
Rob earned 400 total points
ID: 40349162
Following on from Leakim971's posts, it's a good idea to use a namespace (I touch on it in my article "Namespaces" in my article: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_13138-Javascript-is-just-an-Object.html)

//var myproject = {}; // empty object

var addRow= function(view){
myproject.myGlobalvar = null;
...
...

myproject.myGlobalvar = view;
}

Open in new window

0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question