![](/img/trans.png)
[英]How to reduce execution time of this script (Google app script timeout)
[英]How to ignore Execution time of Google script and JS
我在这里有for循环,用于从ticketmaster网站调用API调用并导入谷歌表格。 问题是我不能一次进行许多 API 调用,我必须等待大约 1 秒。
因此,当我让循环等待 1 秒时,例如,如果数组有 40 个元素,我等待 40 秒。 由于数据量大,这没有问题,但是当函数等待它给出执行时间限制的错误时,我需要将执行时间设置为无限。
for (var Veunue_id1 = 0; Veunue_id1 < Venue_Id_List.length; Veunue_id1++) {
var Venue_API_Url = "https://app.ticketmaster.com/discovery/v2/venues?apikey=" + API_key + "&keyword=" + Venue_Id_List[Veunue_id1] + "&locale=*";
// ImportJSON(url, "/","noInherit,noTruncate,rawHeaders");
// console.log(ImportJSON(url, "/", "noInherit,noTruncate,rawHeaders"));
// console.log("Veuneid" + Veunue_id + Venue_Id_List.length);
results = results.concat(ImportJSON(Venue_API_Url, "/_embedded/venues/id", "noInherit,noTruncate,rawHeaders"));
console.log(results);
// wait1 second
Utilities.sleep(1000);
}
这是一个常见问题,尤其是在使用大量数据 API 时。 一般做法是:
基于您的情况的示例代码(未经测试)
function SimpleTimer(timeout){
var start = Date.now();
this.getElapsed = () => Date.now() - start;
this.isTimeout = () => this.getElapsed() > timeout;
}
function resetProgress(){
PropertiesService.getScriptProperties().setProperty('last', '-1')
}
function test_SimpleTimer() {
var timer = new SimpleTimer(4*60*1000) // 4 min;
var start = PropertiesService.getScriptProperties().getProperty('last') || '-1';
for (var Veunue_id1 = parseInt(start) + 1; Veunue_id1 < Venue_Id_List.length && ! timer.isTimeout(); Veunue_id1++) {
var Venue_API_Url = "https://app.ticketmaster.com/discovery/v2/venues?apikey=" + API_key + "&keyword=" + Venue_Id_List[Veunue_id1] + "&locale=*";
results = results.concat(ImportJSON(Venue_API_Url, "/_embedded/venues/id", "noInherit,noTruncate,rawHeaders"));
Utilities.sleep(1000);
PropertiesService.getScriptProperties().setProperty('last', '' + Veunue_id1)
}
// SAVE YOUR DATA HERE, NEXT CALL WILL PROCEED FROM LAST STEP
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.