Solved

javascript global variable not updating

Posted on 2014-09-28
3
298 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
  • 2
3 Comments
 
LVL 82

Accepted Solution

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

Expert Comment

by:Rob Jurd, EE MVE
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 42

Assisted Solution

by:Rob Jurd, EE MVE
Rob Jurd, EE MVE earned 100 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

825 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