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.