Solved

javascript global variable not updating

Posted on 2014-09-28
3
292 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

930 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now