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!
}

1 Answer 1

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');
}
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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?
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.