admin管理员组文章数量:1418594
I'm doing a simple script for detecting when it's 12 o'clock, that it shows a certain div, if not that it remains hidden. So the general idea is this:
function displayDiv() {
d = new Date();
if(12 === d.getHours()){
idMain.style.display = 'block';
} else {
idMain.style.display = 'none';
}
}
But I want that in an event listener so when it's that hour it automatically displays the div or hides it. Something like:
document.addEventListener(d.getHour() === 12, displayDiv);
Or something like that but I don't know how to establish the parameters to get the desired response or for it to work.
Thanks
I'm doing a simple script for detecting when it's 12 o'clock, that it shows a certain div, if not that it remains hidden. So the general idea is this:
function displayDiv() {
d = new Date();
if(12 === d.getHours()){
idMain.style.display = 'block';
} else {
idMain.style.display = 'none';
}
}
But I want that in an event listener so when it's that hour it automatically displays the div or hides it. Something like:
document.addEventListener(d.getHour() === 12, displayDiv);
Or something like that but I don't know how to establish the parameters to get the desired response or for it to work.
Thanks
Share Improve this question asked Aug 14, 2018 at 17:35 SistemasIntegralesSistemasIntegrales 892 silver badges11 bronze badges 1- Do you care if the time is noon their time, or if it is noon your time? Or should it be noon GMT? – Taplar Commented Aug 14, 2018 at 17:40
3 Answers
Reset to default 5You can use a setInterval
to run the function every minute (60000 milliseconds).
function displayDiv() {
d = new Date();
if(12 === d.getHours()){
idMain.style.display = 'block';
} else {
idMain.style.display = 'none';
}
}
setInterval(displayDiv, 60000);
I pletely agree with Hev1 solution of using the setInterval
(use his implementation). Here is some additional information though:
setInterval takes 2 arguments. The first argument is the callback function which you want to execute at a specific time interval. And the second argument is the amount of miliseconds you want the function to execute.
Also I want to point out if you ever are in need of clearing the interval there is also a function for this call clearInterval
. You have to store the interval in an variable for this, here is an example:
const myInterval = setInterval(increase, 1000);
let index = 5;
function increase() {
if (index === 0) {
clear();
} else {
console.log(index);
index--;
}
}
function clear() {
clearInterval(myInterval);
console.log('interval cleared');
}
After the interval is stored in a variable the clear()
method gets called when the interval reaches 0. In the clear function clearInterval
is called to remove the interval.
We can put the if condition using following current time values. var dates = new Date(); var hours = dates.getHours(); var minutes = dates.getMinutes();
本文标签: javascriptEvent listener for detecting hourStack Overflow
版权声明:本文标题:javascript - Event listener for detecting hour - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745289929a2651730.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论