Can anyone help me understand why my triggers are often executing too early?
Here is a snippet of my code, where next
is a variable, in seconds, informing when my trigger should next run:
ScriptApp.newTrigger('passiveCheck')
.timeBased()
.after(next * 1000)
.create();
Logger.log('Target time: ' + targetTime(next));
I have tried calculating then logging the timestamp as to when the trigger should run, and my trigger very frequently runs early, sometimes by up to ~45 seconds. My understanding from the documentation is that the delay clock trigger should never run early, but might run late?
I've included targetTime
for context:
function targetTime(next) {
//Get the current date
var now = new Date();
//Convert into a timestamp
now = now.getTime();
//Convert seconds to milliseconds
next = next * 1000;
//Determine the target timestamp for execution
var target = now + next;
target = new Date(target);
//Format timestamp for logging
target = Utilities.formatDate(target, Session.getScriptTimeZone(), 'yyyy-mm-dd HH:mm:ss');
return target;
}
Thanks in advance!