/    Sign up×
Community /Pin to ProfileBookmark

Global variables

Can someone explain what I am doing wrong with what I think is a global variable

the file reads OK and MyText inside MyFunction contains the value from the file but after exiting the function MyText disappears and I get “undefined”!

[CODE]
<html>
<head>
<script src=”http://code.jquery.com/jquery-latest.js”></script>
<script type=’text/javascript’>
var MyText;
function MyFunction(){
jQuery.get(‘test.txt’, function(data) {
MyText = data;
});
}
</script>
</head>
<body>
<script type=’text/javascript’>
MyFunction();
alert(MyText);
</script>
</body>
</html>
[/CODE]

Thanks

Steve

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinAug 17.2012 — you have a timing problem

the var MyText is in

the global scope

however

you access it Before

jquery has updated it

here is the solution ...



[CODE]<!doctype html>
<html>
<head>

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type='text/javascript'>
var MyText = "";
function MyFunction(){
jQuery.ajaxSetup({async:false});
jQuery.get('test.txt', function(data) {
MyText = data;
});
}
</script>
</head>
<body>
<script type='text/javascript'>
MyFunction();
alert(MyText)
</script>
</body>
</html>[/CODE]


using jQuery.ajaxSetup({async:false});

causes the script to

wait until MyText has

been updated
Copy linkTweet thisAlerts:
@scarr999authorAug 17.2012 — EDIT:Fixed it, needed to convert from text to float ?, thanks for all your help


DaveyErwin your a star ?

Next problem lol, here you can see i am trying to use MyText as a variable in the Google gauge, surprise surprise it does not work (because I'm a newbie!!)

I know the value (or should I say text) is OK at 0.9.

Any ideas?


[CODE]
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type='text/javascript'>
var MyText;
function MyFunction(){
jQuery.ajaxSetup({async:false});
jQuery.get('test.txt', function(data) {
MyText = data;
});
}
</script>

<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<script type='text/javascript'>
google.load('visualization', '1', {packages:['gauge']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Label', 'Value'],
['kW', MyText]
]);

var options = {
animation:{
duration: 1000,
easing: 'linear',
},
width: 1200, height: 360,
greenFrom: 0.7, greenTo: 1.5,
redFrom: 1.5, redTo: 2.0,
minorTicks: 0.1,
min: 0,max: 2.0
};

var chart = new google.visualization.Gauge(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>

</head>
<body>
<div id='chart_div'></div>
<script type='text/javascript'>

MyFunction();
</script>
</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@daveyerwinAug 17.2012 — [CODE]<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type='text/javascript'>
var MyText;
function MyFunction(){
jQuery.ajaxSetup({async:false});
jQuery.get('test.txt', function(data) {
MyText = data;
});
}
</script>

<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<script type='text/javascript'>
google.load('visualization', '1', {packages:['gauge']});
google.setOnLoadCallback(drawChart);
function drawChart() {alert(MyText)
var data = google.visualization.arrayToDataTable([
['Label', 'Value'],
['kW', [COLOR="Magenta"]Number(MyText)[/COLOR] ]
]);

var options = {
animation:{
duration: 1000,
easing: 'linear',
},
width: 1200, height: 360,
greenFrom: 0.7, greenTo: 1.5,
redFrom: 1.5, redTo: 2.0,
minorTicks: 0.1,
min: 0,max: 2.0
};

var chart = new google.visualization.Gauge(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>

</head>
<body>
<div id='chart_div'></div>
<script type='text/javascript'>

MyFunction();
</script>

</body>
</html>[/CODE]
×

Success!

Help @scarr999 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...