I am receiving a syntax error with unexpected token < at line 14 when i try to run this script in my browser. What i am trying to do is open my classes 5 minutes before class and still open when ran all the way up to 5 minutes before the next class.
JavaScript
x
32
32
1
<script>
2
//Current time
3
var date = new Date();
4
var time = date.getTime();
5
6
//Time checker
7
if(time >= 7:55 && < 8:55){
8
window.open('https://classroom.google.com/c/MTIyMjc3NTE0MzEw');
9
}
10
if(time >= 8:55 && < 9:55){
11
window.open('https://classroom.google.com/c/MTE1MjA4MzM5MDgz');
12
}
13
if(time >= 9:55 && < 10:55){
14
window.open('https://classroom.google.com/c/MTIyMjk4MzAxMjYx');
15
}
16
if(time >= 10:55 && < 11:55){
17
window.open('https://classroom.google.com/c/MTIzMjMyNzU4ODk2');
18
}
19
if(time >= 11:55 && < 12:55){
20
window.open('https://classroom.google.com/c/MTIzMTkzMjU1MjAx');
21
}
22
if(time >= 12:55 && < 13:55){
23
window.open('https://classroom.google.com/c/MTIyMjk4MzAxMjQx');
24
}
25
if(time >= 13:55 && < 14:55){
26
window.open('https://classroom.google.com/c/MTIyNDk3Mjk5NDQ2');
27
}
28
if(time >= 14:55 && <= 15:00){
29
window.open('https://classroom.google.com/c/MTIyNjk1NTQxMzYw');
30
}
31
</script>
32
Advertisement
Answer
date.getTime()
doesn’t return the time of day. If you want the time of day, use date.getHours()
and date.getMinutes()
. You can then convert this to the number of minutes since midnight, which you can use to tell if the time is within your specific ranges.
JavaScript
1
29
29
1
//Current time
2
var date = new Date();
3
var time = 60 * date.getHours() + date.getMinutes();
4
5
//Time checker
6
if(time >= (7 * 60 + 55) && time < (8 * 60 + 55)){
7
window.open('https://classroom.google.com/c/MTIyMjc3NTE0MzEw');
8
}
9
if(time >= (8 * 60 + 55) && time < (9 * 60 + 55)){
10
window.open('https://classroom.google.com/c/MTE1MjA4MzM5MDgz');
11
}
12
if(time >= (9 * 60 + 55) && time < (10 * 60 + 55)){
13
window.open('https://classroom.google.com/c/MTIyMjk4MzAxMjYx');
14
}
15
if(time >= (10 * 60 + 55) && time < (11 * 60 + 55)){
16
window.open('https://classroom.google.com/c/MTIzMjMyNzU4ODk2');
17
}
18
if(time >= (11 * 60 + 55) && time < (12 * 60 + 55)){
19
window.open('https://classroom.google.com/c/MTIzMTkzMjU1MjAx');
20
}
21
if(time >= (12 * 60 + 55) && time < (13 * 60 + 55)){
22
window.open('https://classroom.google.com/c/MTIyMjk4MzAxMjQx');
23
}
24
if(time >= (13 * 60 + 55) && time < (14 * 60 + 55)){
25
window.open('https://classroom.google.com/c/MTIyNDk3Mjk5NDQ2');
26
}
27
if(time >= (14 * 60 + 55) && time < (15 * 60 + 55)){
28
window.open('https://classroom.google.com/c/MTIyNjk1NTQxMzYw');
29
}