-
Notifications
You must be signed in to change notification settings - Fork 0
/
_schedule.js
56 lines (47 loc) · 1.36 KB
/
_schedule.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// ref: https://www.itread01.com/p/1421631.html
var tHours = 0;
var tMinutes = 0;
var tSeconds = 0;
var timer = null;
function schedule(hours, minutes, seconds) {
tHours = hours;
tMinutes = minutes;
tSeconds = seconds;
// TODO check..
run();
}
function run() {
var date = new Date();
if(checkTargetTime(date) && (date.getMilliseconds() < 3)) {
toDo();
window.clearTimeout(timer);
} else if(compareTargetTime(date) && date.getMilliseconds() >= 3) {
var pathname = location.pathname;
// do something if match route
window.clearTimeout(timer);
} else {
timer = window.setTimeout(run, 1);
}
}
/* --------------------------------------------------------- */
function toDo() {
//Add your operation here
console.log('do some thing');
}
/* --------------------------------------------------------- */
function checkTargetTime(date) {
return (date.getHours() - tHours == 0) &&
(date.getMinutes() - tMinutes == 0) &&
(date.getSeconds() - tSeconds == 0);
}
function compareTargetTime(date) {
//debug:console.log('compare target time');
return (date.getHours() * 3600 + date.getMinutes() * 60 + date.getSeconds())
>= (tHours * 3600 + tMinutes * 60 + tSeconds);
}
// 執行腳本
function runScript() {
console.log('start running script');
schedule(tHours, tMinutes, tSeconds);
}
runScript();