Using a Script created in Google Drive:
Code.gs:
function doGet() { return HtmlService.createTemplateFromFile('page') .evaluate() .setSandboxMode(HtmlService.SandboxMode.NATIVE); } var date = new Date(); var last = -1; function func(value) { Utilities.sleep(100); var out = "date = " + date + ", last = " + last; last = value; return out; }
page.html:
<style type="text/css"> pre { border: 1px solid green; } </style> <div id="out"> <pre>loading...</pre> </div> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script> $(function() { for(var i = 0; i < 2; i++) { launch(i); } }); setTimeout(function() { for(var i = 2; i < 4; i++) { launch(i); } }, 2000); function launch(id) { $("<pre />").text("started " + id).appendTo('#out'); google.script.run .withSuccessHandler(function(result) { $("<pre />").text("finished " + id + ": " + result).appendTo('#out'); }) .func(id); } </script>
This output was produced:
loading... started 0 started 1 finished 0: date = Sat Jan 04 2014 12:03:06 GMT-0400 (AST), last = -1 finished 1: date = Sat Jan 04 2014 12:03:06 GMT-0400 (AST), last = -1 started 2 started 3 finished 2: date = Sat Jan 04 2014 12:03:08 GMT-0400 (AST), last = -1 finished 3: date = Sat Jan 04 2014 12:03:08 GMT-0400 (AST), last = -1
Which leads me to the conclusion that the server is stateless, at least from the point of view of the scripts running on it.
You can store a value for a user using:
ReplyDeletePropertiesService.getUserProperties().setProperty('last', last);
and get it again using:
var last = PropertiesService.getUserProperties().getProperty('last');