Google Apps脚本触发执行为时已晚 [英] Google Apps Script trigger execution too late
问题描述
我试图在1秒后触发函数执行,但Google在40秒或2分钟后执行该函数.
I'm trying to trigger the execution of a function after like 1 second but google execute the function after 40 seconds or something 2 minutes.
这是我的代码
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();
}
我尝试了 after
之后和 at
函数,但是两者都不起作用.当我查看执行页面时,我得到的历史记录显示了执行 myFunction
和 testFunc
之间的时间.
I tried with the after
and the at
functions but both doesn't work.
When I look in the execution page I have this history showing the time between the execution of myFunction
and testFunc
.
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
时间可能会稍微随机化-例如,如果您创建了一个周期性的上午9点触发器,Apps Script会选择上午9点至上午10点之间的时间.
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之间)执行触发器吗?
And I'm trying to have my trigger execute after just a few seconds (between 5 and 10) is there anyway to do that ?
推荐答案
您对原因是随机次数,但是,没有办法强迫它不成立.
You are right about the cause of the randomized times, however, there is no way to force this to be untrue.
您有两种选择:
- 考虑是否真的需要在调用其他函数之前等待.(如果您要测试主要功能上的数据操作是否有效,则无需等待.)
- 使用
Utilities.sleep()
添加您的延迟时间.请注意,这可能需要6分钟的执行时间.
希望这会有所帮助!
这篇关于Google Apps脚本触发执行为时已晚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!