I have a script function, and I created a trigger to run every hour
I need to stop that trigger at specific day and times
for example I don't want to run it from Friday 5pm until Saturday 10pm
I found a function to stop it by day
//Skip week-end
var day = new Date();
if (day.getDay()>5 || day.getDay()==0) {
return;
}
but I don't know how to accomplish to be more specific from Friday 5pm until Saturday 10pm to don't run it
any help please ?
You might want to include a check inside the trigger function to skip processing if the date and time fall in the specified period.
function shouldRunTrigger() {
var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var date = new Date();
var day = days[date.getDay()];
var hours = date.getHours();
if ((day === "Fri" && hours >= 17) || (day === "Sat" && hours <= 22)) {
return false;
}
return true;
}
function myTrigger() {
if (!shouldRunTrigger()) return;
// trigger code here
}
I think there is the simplest way it's skip current execution without re-create the trigger.
Suppose the days are hundreds:
- Friday is
500
- Saturday -
600
Suppose the hours are dozens and units:
- 01:00 -
1
- 17:00 -
17
- 22:00 -
22
Then we have to pause between 500 + 17
and up to 600 + 22
.
function shouldRunTrigger() {
var now = new Date();
var k = now.getDay() * 100 + now.getHours() + now.getMinutes()/100;
return !(k >= 517 && k < 622);
}
function myTrigger() {
if (!shouldRunTrigger()) return; // If true then skip it
// trigger code here
}