【问题标题】:Google Apps Script trigger execution too lateGoogle Apps 脚本触发器执行太晚
【发布时间】:2019-10-15 12:33:39
【问题描述】:

我试图在 1 秒后触发函数的执行,但谷歌在 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 这么说

时间可能会稍微随机化 - 例如,如果您创建一个重复的上午 9 点触发器,Apps 脚本会选择上午 9 点到 10 点之间的时间。

我正试图让我的触发器在几秒钟后(5 到 10 秒之间)执行,有什么办法吗?

【问题讨论】:

    标签: google-apps-script google-sheets triggers google-apps


    【解决方案1】:

    您对cause of the randomized times 的看法是正确的,但是,没有办法强迫这是不真实的。

    你有几个选择:

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

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多