1
|
|
|
document.addEventListener("DOMContentLoaded", function() { |
2
|
|
|
(function timeAgo(selector) { |
|
|
|
|
3
|
|
|
|
4
|
|
|
var templates = { |
5
|
|
|
prefix: "", |
6
|
|
|
suffix: " ago", |
7
|
|
|
seconds: "less than a minute", |
8
|
|
|
minute: "about a minute", |
9
|
|
|
minutes: "%d minutes", |
10
|
|
|
hour: "about an hour", |
11
|
|
|
hours: "about %d hours", |
12
|
|
|
day: "a day", |
13
|
|
|
days: "%d days", |
14
|
|
|
month: "about a month", |
15
|
|
|
months: "%d months", |
16
|
|
|
year: "about a year", |
17
|
|
|
years: "%d years" |
18
|
|
|
}; |
19
|
|
|
var template = function(t, n) { |
20
|
|
|
return templates[t] && templates[t].replace(/%d/i, Math.abs(Math.round(n))); |
21
|
|
|
}; |
22
|
|
|
|
23
|
|
|
var timer = function(time) { |
24
|
|
|
if (!time) |
25
|
|
|
return; |
|
|
|
|
26
|
|
|
time = time.replace(/\.\d+/, ""); // remove milliseconds |
27
|
|
|
time = time.replace(/-/, "/").replace(/-/, "/"); |
28
|
|
|
time = time.replace(/T/, " ").replace(/Z/, " UTC"); |
29
|
|
|
time = time.replace(/([\+\-]\d\d)\:?(\d\d)/, " $1$2"); // -04:00 -> -0400 |
30
|
|
|
time = new Date(time * 1000 || time); |
31
|
|
|
|
32
|
|
|
var now = new Date(); |
33
|
|
|
var seconds = ((now.getTime() - time) * .001) >> 0; |
34
|
|
|
var minutes = seconds / 60; |
35
|
|
|
var hours = minutes / 60; |
36
|
|
|
var days = hours / 24; |
37
|
|
|
var years = days / 365; |
38
|
|
|
|
39
|
|
|
return templates.prefix + ( |
40
|
|
|
seconds < 45 && template('seconds', seconds) || |
41
|
|
|
seconds < 90 && template('minute', 1) || |
42
|
|
|
minutes < 45 && template('minutes', minutes) || |
43
|
|
|
minutes < 90 && template('hour', 1) || |
44
|
|
|
hours < 24 && template('hours', hours) || |
45
|
|
|
hours < 42 && template('day', 1) || |
46
|
|
|
days < 30 && template('days', days) || |
47
|
|
|
days < 45 && template('month', 1) || |
48
|
|
|
days < 365 && template('months', days / 30) || |
49
|
|
|
years < 1.5 && template('year', 1) || |
50
|
|
|
template('years', years) |
51
|
|
|
) + templates.suffix; |
52
|
|
|
}; |
53
|
|
|
|
54
|
|
|
var elements = document.getElementsByClassName('timeago'); |
55
|
|
|
for (var i = 0; i < elements.length; ++i) { |
56
|
|
|
var $this = elements[i]; |
57
|
|
|
if (typeof $this === 'object') { |
58
|
|
|
$this.innerHTML = timer($this.getAttribute('data-datetime')); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
// update time every minute |
62
|
|
|
setTimeout(timeAgo, 60000); |
63
|
|
|
|
64
|
|
|
})(); |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
}); |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.