Skip to content

Instantly share code, notes, and snippets.

@bennettscience
Last active November 16, 2017 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennettscience/dda1b3c730ede7d45a6518046cb88c13 to your computer and use it in GitHub Desktop.
Save bennettscience/dda1b3c730ede7d45a6518046cb88c13 to your computer and use it in GitHub Desktop.
Prevent script timeouts in Google Apps Script
function isTimeUp_(starttime) {
var now = new Date();
return now.getTime() - starttime.getTime() > 270000; // 4.5 minutes
}
// Set the start time outside the loop
var starttime = new Date();
// Inside your function, check the time and break to prevent failure mid-loop
for(var i=0; i<data.length; i++) {
if(isTimeUp_(starttime)) {
break;
}
// continue loop
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment