I'd love to see if other variants of the idea could be implemented. Can you close a site for certain hours? The entire year except one month, one week, one week, one hour, one minute?
Of course, sounds kinda counterproductive, but it could lead to some fun ideas, methinks
Definitely - the API reference for
the Date API will have more info if you want to try other variants, but here's a modified version that lets you play around with them!
First, the base script, which is a modified version of Melon's:
// Simple Site Closer script v0.1
// This script shuts down certain pages automatically on some days of the week so you can close your site!
//
// How to use:
// Just place this script in the head of any page you'd like to close!
// Make a page called closed.html and put your closed info on there!
// Add this code after you link the script:
// <script>
// // sc.closedPage = "closed.html"; // Optional: change the closed page url
// // sc.closedDays = ["Thursday"]; // Optional: change the days to close (default is Monday)
// shutItDown();
// </script>
//
// You can also set a function that returns a boolean indicating whether the site is closed:
// <script>
// sc.isSiteClosed = function (now) {
// // Example: Check if the current hour is greater than 8:00 p.m. local time
// return now.getHours() >= 20;
// }
// </script>
// This will override the default check, which looks at the current day.
var sc = {};
sc.closedPage = "closed.html";
sc.closedDays = ["Monday"];
sc.weekDays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
sc.isSiteClosed = function (now) {
sc.dayNow = sc.weekDays[now.getDay()];
return sc.closedDays.includes(sc.dayNow);
}
// Trigger this site closer!
function shutItDown() {
if (sc.isSiteClosed(new Date())) {
console.log("Gordon Ramsay screams: Shut it Down!!!");
window.location.replace(sc.closedPage);
}
}
After including this script on your site, you would set the
sc.isSiteClosed property instead of
sc.closedDays, assigning it to a function that takes a
Date object and returns a boolean. The boolean indicates whether the site is currently closed (true) or not (false).
For example, this checks if it's 8:00 p.m. or later:
sc.isSiteClosed = function (now) {
return now.getHours() >= 20;
}
shutItDown();
Or this could check if it's currently between 12:00 and 12:30 p.m., to simulate a lunch break:
sc.isSiteClosed = function (now) {
return now.getHours() === 12 && now.getMinutes() < 30;
}
shutItDown();
Haven't tested this yet so if you have any issues with it let me know!