2019-09-30 15:38:41 +03:00
|
|
|
/*
|
2021-01-23 10:07:18 +03:00
|
|
|
* Calculate the Timeago
|
2020-01-02 16:17:49 +03:00
|
|
|
* v2.0
|
|
|
|
* https://github.com/cotes2020/jekyll-theme-chirpy
|
2019-09-30 15:38:41 +03:00
|
|
|
* © 2019 Cotes Chung
|
|
|
|
* MIT Licensed
|
|
|
|
*/
|
|
|
|
|
|
|
|
$(function() {
|
|
|
|
|
2021-01-23 10:07:18 +03:00
|
|
|
const timeagoElem = $(".timeago");
|
2020-08-19 07:26:45 +03:00
|
|
|
|
2021-01-23 10:07:18 +03:00
|
|
|
let toRefresh = timeagoElem.length;
|
|
|
|
|
|
|
|
let intervalId = void 0;
|
2020-08-19 07:26:45 +03:00
|
|
|
|
2020-07-23 10:33:42 +03:00
|
|
|
function timeago(iso, isLastmod) {
|
|
|
|
let now = new Date();
|
|
|
|
let past = new Date(iso);
|
|
|
|
|
2020-08-19 07:26:45 +03:00
|
|
|
if (past.getFullYear() !== now.getFullYear()) {
|
2020-07-23 10:33:42 +03:00
|
|
|
toRefresh -= 1;
|
|
|
|
return past.toLocaleString("en-US", {
|
2020-08-19 07:26:45 +03:00
|
|
|
year: "numeric",
|
|
|
|
month: "short",
|
|
|
|
day: "numeric"
|
2020-07-23 10:33:42 +03:00
|
|
|
});
|
2019-09-30 15:38:41 +03:00
|
|
|
}
|
|
|
|
|
2020-08-19 07:26:45 +03:00
|
|
|
if (past.getMonth() !== now.getMonth()) {
|
2020-07-23 10:33:42 +03:00
|
|
|
toRefresh -= 1;
|
|
|
|
return past.toLocaleString("en-US", {
|
2020-08-19 07:26:45 +03:00
|
|
|
month: "short",
|
|
|
|
day: "numeric"
|
2020-07-23 10:33:42 +03:00
|
|
|
});
|
2019-09-30 15:38:41 +03:00
|
|
|
}
|
|
|
|
|
2020-07-23 10:33:42 +03:00
|
|
|
let seconds = Math.floor((now - past) / 1000);
|
2019-09-30 15:38:41 +03:00
|
|
|
|
2020-07-23 10:33:42 +03:00
|
|
|
let day = Math.floor(seconds / 86400);
|
2019-09-30 15:38:41 +03:00
|
|
|
if (day >= 1) {
|
2020-07-23 10:33:42 +03:00
|
|
|
toRefresh -= 1;
|
2019-09-30 15:38:41 +03:00
|
|
|
return day + " day" + (day > 1 ? "s" : "") + " ago";
|
|
|
|
}
|
|
|
|
|
2020-07-23 10:33:42 +03:00
|
|
|
let hour = Math.floor(seconds / 3600);
|
2019-09-30 15:38:41 +03:00
|
|
|
if (hour >= 1) {
|
|
|
|
return hour + " hour" + (hour > 1 ? "s" : "") + " ago";
|
|
|
|
}
|
|
|
|
|
2020-07-23 10:33:42 +03:00
|
|
|
let minute = Math.floor(seconds / 60);
|
2019-09-30 15:38:41 +03:00
|
|
|
if (minute >= 1) {
|
|
|
|
return minute + " minute" + (minute > 1 ? "s" : "") + " ago";
|
|
|
|
}
|
|
|
|
|
2020-08-19 07:26:45 +03:00
|
|
|
return (isLastmod ? "just" : "Just") + " now";
|
2019-09-30 15:38:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateTimeago() {
|
|
|
|
$(".timeago").each(function() {
|
|
|
|
if ($(this).children("i").length > 0) {
|
2021-01-23 10:07:18 +03:00
|
|
|
$(this).text();
|
|
|
|
let isLastmod = $(this).hasClass("lastmod");
|
|
|
|
let node = $(this).children("i");
|
|
|
|
let date = node.text(); /* ISO Date: "YYYY-MM-DDTHH:MM:SSZ" */
|
2019-09-30 15:38:41 +03:00
|
|
|
$(this).text(timeago(date, isLastmod));
|
|
|
|
$(this).append(node);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-08-19 07:26:45 +03:00
|
|
|
if (toRefresh === 0 && typeof intervalId !== "undefined") {
|
|
|
|
clearInterval(intervalId); /* stop interval */
|
2019-09-30 15:38:41 +03:00
|
|
|
}
|
2020-07-23 10:33:42 +03:00
|
|
|
return toRefresh;
|
2019-09-30 15:38:41 +03:00
|
|
|
}
|
|
|
|
|
2020-08-19 07:26:45 +03:00
|
|
|
if (toRefresh === 0) {
|
2019-09-30 15:38:41 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-23 10:33:42 +03:00
|
|
|
if (updateTimeago() > 0) { /* run immediately */
|
2020-08-19 07:26:45 +03:00
|
|
|
intervalId = setInterval(updateTimeago, 60000); /* run every minute */
|
2019-09-30 15:38:41 +03:00
|
|
|
}
|
|
|
|
|
2020-08-19 07:26:45 +03:00
|
|
|
});
|