- Avoid putting code in the HTML. eg you have
<body onload="startTime()">
which can be done in the script withaddEventListener("load",startTime)
But its not really needed as you have the interval call the function anyway. - You can access elements directly via their
id
as long as you ensure that the id is unique on the page. If you use the same name for any otherid
orname
element property then it will not work. Refer to answers to this SO post for more context. - When adding text only (no HTML) to an element it is more efficient to add it via the textContent property. For simple stuff it's not a major issue, itsit is when you start to add lots of content that it will be noticablenoticeable.
The value of a Date object is aan integer that counts the milliseconds since 1 January 1970 UTC.
The example below show how it can be done using millisonsmilliseconds alone.
- Avoid putting code in the HTML. eg you have
<body onload="startTime()">
which can be done in the script withaddEventListener("load",startTime)
But its not really needed as you have the interval call the function anyway. - You can access elements directly via their
id
as long as you ensure that the id is unique on the page. If you use the same name for any otherid
orname
element property then it will not work. - When adding text only (no HTML) to an element it is more efficient to add it via the textContent property. For simple stuff it's not a major issue, its when you start to add lots of content that it will be noticable.
The value of a Date object is a integer that counts the milliseconds since 1 January 1970 UTC.
The example below show how it can be done using millisons alone.
- Avoid putting code in the HTML. eg you have
<body onload="startTime()">
which can be done in the script withaddEventListener("load",startTime)
But its not really needed as you have the interval call the function anyway. - You can access elements directly via their
id
as long as you ensure that the id is unique on the page. If you use the same name for any otherid
orname
element property then it will not work. Refer to answers to this SO post for more context. - When adding text only (no HTML) to an element it is more efficient to add it via the textContent property. For simple stuff it's not a major issue, it is when you start to add lots of content that it will be noticeable.
The value of a Date object is an integer that counts the milliseconds since 1 January 1970 UTC.
The example below show how it can be done using milliseconds alone.
##Bug
Bug
##Some coding points
Some coding points
##Regarding time
Regarding time
##Example snippet
Example snippet
##setInterval
V setTimeout
setInterval
V setTimeout
##Bug
##Some coding points
##Regarding time
##Example snippet
##setInterval
V setTimeout
Bug
Some coding points
Regarding time
Example snippet
setInterval
V setTimeout
##Bug
There is an off chance that the interval event that you start at the bottom of the script with
setInterval(startTime, 1000);
will fire before the page has fully loaded, causing the first run to throw an error atdocument.getElementById("message").innerHTML =
However it is a minor bug and will fix itself in the next interval.
##Some coding points
- Avoid putting code in the HTML. eg you have
<body onload="startTime()">
which can be done in the script withaddEventListener("load",startTime)
But its not really needed as you have the interval call the function anyway. - You can access elements directly via their
id
as long as you ensure that the id is unique on the page. If you use the same name for any otherid
orname
element property then it will not work. - When adding text only (no HTML) to an element it is more efficient to add it via the textContent property. For simple stuff it's not a major issue, its when you start to add lots of content that it will be noticable.
##Regarding time
The value of a Date object is a integer that counts the milliseconds since 1 January 1970 UTC.
Since you are not interested in the specific time, but rather half hour periods it can be simpler to work in milliseconds rather than the more complex minutes, seconds format.
To get the millisecond value you can use Date.now()
and to change that value to half hour chunks you get the remainder of dividing by 30 * 60 * 1000ms using the remainder operator %
The remainder operator %
is great when you need to get cyclic values.
##Example snippet
The example below show how it can be done using millisons alone.
addEventListener("load", displayPeriod); // start on page load
const second = 1000; // length of second in ms
const minute = 60 * second; // length of minute in ms
const halfHour = 30 * minute; // length of half hour in ms
function displayPeriod() {
const halfHourTime = Date.now() % halfHour;
var nextPeriod = 25 * minute;
if (halfHourTime <= nextPeriod) {
message.textContent = "WORK TIME";
} else {
message.textContent = "BREAK TIME";
nextPeriod = halfHour;
}
const min = Math.floor((nextPeriod - halfHourTime) / minute);
const sec = Math.floor((nextPeriod - halfHourTime) / second) % 60;
countDown.textContent = min + ":" + ("" + sec).padStart(2,"0");
var time2Second = second - halfHourTime % second;
if (time2Second < 50) { // if too close delay a littl
time2Second += 50;
}
setTimeout(displayPeriod, time2Second);
}
div{
font-family : arial;
font-size : 32px;
width : 100%;
text-align : center;
}
<div id="message"></div>
<div id="countDown"></div>
Or using interval and shortening the naming as I find long names hard on the eyes. Also I use a shortcut for Math.floor
the bitwise | 0
which is handy for rounding down positive numbers. I also used the ms times expressed with the exponent as I happen to know them (3e5 is 5 minutes in milliseconds)
I would consider the second snippet of lower quality, but for simple projects less code can be better.
addEventListener("load", () => setInterval(displayPeriod,1000)); // start on page load
function displayPeriod() {
const time= Date.now() % 18e5;
var nextP = 15e5;
var mes = "WORK TIME :(";
if (time > nextP) {
mes = "BREAK TIME :)";
nextP = 18e5;
}
const m = (nextP - time) / 6e4 | 0;
const s = ("" + ((nextP - time) / 1e3 | 0) % 60).padStart(2,"0");
message.textContent = mes;
countDown.textContent = m + ":" + s;
}
div{
font-family : arial;
font-size : 32px;
width : 100%;
text-align : center;
}
<div id="message"></div>
<div id="countDown"></div>
##setInterval
V setTimeout
Personally I prefer using setTimeout
rather than the setInterval
as it give finer control over when the next event will fire. In the example I use the ms time to workout how long it is till the next second starts, if too close to ensure the timer will fire (browsers may throttle timers) I offset it a little.
This method sets the event for the closest coming second, and will skip any missed seconds.
But setInterval
is just as good but if the system is busy the countdown can skip displaying seconds more often than using setTimeout