1

I try to use a global variable in my function, see below. I can use the global variable in my func1 to store a value, for Ex. 77, ok. In func2 I try to use the current value that is stored in the global variable but the result is undefined.

Any advice how to get this working?

doGet() {
    ...
    var buttonValue;
    ...

func1(e,buttonValue) {
    ...
    buttonValue = size;
    throw buttonValue;
    --> buttonValue ok!
    ...
}

func2(e,buttonValue) {
    ...
    throw buttonValue;
    --> buttonValue undefined!
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
user1598835
  • 97
  • 2
  • 14

1 Answers1

1

You cannot do that. The value of global variables cannot be changed in handler functions etc. Use CacheService to store values that have a global scope

Here is a code example:

function func1(){
  CacheService.getPrivateCache().put('var_name', 'var_value');
}

function func2(){
  var var_value = CacheService.getPrivateCache().get('var_name');
}
Srik
  • 7,907
  • 2
  • 20
  • 29
  • doGet() { ... var cache = CacheService.getPrivateCache(); ... func1(e) { ... cache.put('foo', size); ... ---> ReferenceError:"cache" is not defined. How should CacheService be used? Actually I just try to get a value out of handler function to global scope so any other solution would help me too. – user1598835 Aug 25 '12 at 08:51
  • It does not work in my code: TypeError:Cannot find function put in object CacheService. I also tried to use var CacheService = CacheService.getPrivateCache(); in doGet() but it didnt help. Am I missing some point? – user1598835 Aug 26 '12 at 08:39
  • @Srik this worked for me as well, a nice way to get text input into a kind of Global variable available during the app session. – Jacobvdb Jan 23 '13 at 00:24