www.webdeveloper.com
Results 1 to 4 of 4

Thread: Global variables

  1. #1
    Join Date
    Aug 2012
    Posts
    8

    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>
    Thanks

    Steve

  2. #2
    Join Date
    Jul 2011
    Posts
    24
    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>
    using jQuery.ajaxSetup({async:false});
    causes the script to
    wait until MyText has
    been updated

  3. #3
    Join Date
    Aug 2012
    Posts
    8

    Yippeeee!!!!!

    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>
    Last edited by scarr999; 08-17-2012 at 08:30 AM.

  4. #4
    Join Date
    Jul 2011
    Posts
    24
    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', Number(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>
    Last edited by DaveyErwin; 08-17-2012 at 09:49 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles