Solved

javascript global variable not updating

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

759 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

21 Experts available now in Live!

Get 1:1 Help Now