简体   繁体   中英

google side bar - are js server side global variables shared between side bar calles

Consider side bar simple server side script.

const publicArr = [];

function doWait2(ms){
   var start = new Date().getTime();
   var end = start;
                 
    while(end < start + ms) {
       end = new Date().getTime();
   }
}

function getfirstCall(){
    publicArr.push("Item1");
    doWait2(10000);
    return publicArr;
}

function getsecondCall(){
    publicArr.push("Item2");
    return publicArr;
}

When calling getfirstCall and getsecondCall one after another, each function returns a single item array (Item1 / Item2 respectively). Changes made by getfirstCall to publicArr are not reflected when client execute getsecondCall .

Note that getfirstCall waits 10 sec before returning allowing getsecondCall to be handled simultaneously by the server. not sure it is important.

Is this guarantee? each client call gets its own instances of the public variables?

Guess this is pretty fundamental with web development, and not only with side bar

In Apps Script global variables are not unique across script executions, you should consider using the PropertiesService to store your publicArr as a script property, retrievable by all users. And perhaps LockService if you are worried about concurrent executions.

See these answers for pointers:

How to define global variable in Google Apps Script

https://stackoverflow.com/a/63601016/4243927

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM