Google Apps 脚本触发执行太迟

Google Apps Script trigger execution too late

我试图在大约 1 秒后触发函数的执行,但 google 在 40 秒或 2 分钟后执行函数。

这是我的代码


function testFunc () {
  Logger.log("test called");
}

function myFunction() {
  const now = Date.now();
  const dateNow = new Date(Date.now());
  const dateLater = new Date(now + 1000);

  const trigg = ScriptApp.newTrigger('testFunc')
    .timeBased()
    .inTimezone("Europe/Paris")
    // .at(dateLater) // 1sec
    .after(1000) // 1sec
    .create();
}

我尝试使用 afterat 函数,但两者都不起作用。 当我查看执行页面时,我有此历史记录显示 myFunctiontestFunc.

执行之间的时间
Head    testFunc    Time-Driven Oct 15, 2019, 2:06:35 PM    0.009 s 
Completed
Head    myFunction  Editor  Oct 15, 2019, 2:05:33 PM    0.589 s 
Completed
Head    testFunc    Time-Driven Oct 15, 2019, 2:04:15 PM    0.202 s 
Completed
Head    myFunction  Editor  Oct 15, 2019, 2:02:57 PM    0.46 s  
Completed

documentation on time driven triggers 表示

The time may be slightly randomized — for example, if you create a recurring 9 a.m. trigger, Apps Script chooses a time between 9 a.m. and 10 a.m.

我正在尝试让我的触发器在几秒后(5 到 10 秒之间)执行,无论如何都可以做到这一点吗?

你说的 cause of the randomized times 是对的,但是,没有办法强制这是不真实的。

您有几个选择:

  1. 考虑一下在调用其他函数之前是否真的需要等待。 (如果您正在测试主函数上的数据操作是否有效,则无需等待。)
  2. 使用 Utilities.sleep() 添加延迟。这可能 运行 超过 6 分钟的执行时间,请小心。

希望对您有所帮助!