Ok some ideas for you. First in bindings is a file globalOptions.ts which looks like this:
export let globalOptions = {
permInState: false,
gpermInState: false,
globalInState: false,
sglobalInState: false
};
Setting globalInState true will make state contain all global variables from server commands, so that is an easy way to update them.
When you add it to g-codes->Response to Event it will get triggered every time it gets the line from Marlin. So easiest is to make marlin send such an info every 3 seconds then, otherwise you must call the function every 3 seconds which can get delays if a slow operation like homin/heating is blocking new commands,
Apart from this you can use these server command in gcode to trigger a function
@timedCall name timeoutMS function_name
Calls function_name after timeoutMS milliseconds. It does not repeat. The name can be used to stop the timer with @deletedTimedCall. If you need an interval, you need to call the function with @timedCall at the end of the function. In that case, make sure it is never started twice, or you end up with increasing number of calls until you have no time left for printing! Best is to always have some exit conditions in the called function like when you wait for a temperature to be reached, but disabling heater would stop loop as well.
@deleteTimedCall Name
Deletes all active monitors with the name Name.
Just define a function with the g-code and set a new time at the end which will call the function again after timeoutMS milliseconds. Delete timer when you disconnect printer. Best location would be in the connection script so it starts as soon as possible. Note that you must deactivate and activate printer so it gets reread and included.