addTimer not working with global app in GAS
Asked Answered
B

1

0

I read about the (undocumented) addTmer and would like to use it for updating the UI.

The example at Auto refresh Google Apps Scripts webapp UI? does work well

// This code works
function doGet(e)
{
 var app = UiApp.createApplication().setTitle('Test addTimer - createApplication in doget');
 var appLocal = app;

  var handler = appLocal.createServerHandler("update");
  appLocal.addTimer(handler , 4000);

  var label = appLocal.createLabel(new Date()).setId("label"); 
  appLocal.add(label);

  return appLocal;
}

function update(e)
{
  var appLocal = UiApp.getActiveApplication();
//  var appLocal = app;
  appLocal.getElementById("label").setText(new Date());
  var handler = appLocal.createServerHandler("update");
  appLocal.addTimer(handler , 1000);

  return appLocal;
}

but if I move createApplication out of doGet() into a global variable, an 'unexpected error' will occur at runtime. Without addTimer it IS possible to move createApplication out of doget() into global scope.

// This code will crash at runtime
 var app = UiApp.createApplication().setTitle('Test addTimer , createApplication outside doGet');

function doGet(e)
{ 
  var appLocal = app;

  var handler = appLocal.createServerHandler("update");
  appLocal.addTimer(handler , 4000);

  var label = appLocal.createLabel(new Date()).setId("label"); 
  appLocal.add(label);

  return appLocal;
}

function update(e)
{
  var appLocal = UiApp.getActiveApplication();
//  var appLocal = app;
  appLocal.getElementById("label").setText(new Date());
  var handler = appLocal.createServerHandler("update");
  appLocal.addTimer(handler , 1000);

  return appLocal;
}

As I've seen global use of

var app = createApplication();

and i suppose more people would like to use addTimer as well, I post my experience here.

Maybe someone can explain WHY this is a problem.

Belemnite answered 20/6, 2014 at 11:45 Comment(0)
R
1

when you call a service outside of any function, this call is executed each time any function is called, that's to say when doGet is called but also when the handler function is called and also when the timer triggers a script execution.

It means that you are calling UiApp.createApplication() 2 (and more) times... this is not a good idea and generates an 'unexpected error'.

by the way, what would be the advantage of that approach ?

Uiapp is designed to work with a main service call and handlers, each handler getting the active instance using getActiveApplication() why are you trying to use it differently?

Ragout answered 20/6, 2014 at 15:7 Comment(2)
Will UiApp.createApplication() really be called X times if you define a global variable var app = UiApp.createApplication(); That would be VERY ineffecicient indeed. The advantage of using a global variable would be you can use 'app' in every function without adding var app = UiApp.getActiveApplication(); in every function. I changed it already =, and it works.Belemnite
Yes indeed, it will run multiple time. You can verify that easily using a spreadsheet and changing a cell color in a "global" call and look at the sheet when any function is called.Ragout

© 2022 - 2024 — McMap. All rights reserved.