This is some sample code that shows how use a design pattern to implement a timer.
To use:
- Create the new Timer instance, indicating time
- call
.body
on the instance, where the first parameter is the function to execute - periodically within that function, call
this.check()
which will raiseTimeUpErr
error if the duration has expired
function myFunction() {
const timer = new Timer({seconds: 2});
timer.body(function () {
for (let x = 0; x < 2000000; x++) {
if (x % 1000 === 0) {
Logger.log('zzzz');
Utilities.sleep(1000);
this.check();
}
}
Logger.log('loop done');
});
}