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
Add a ment  | 

3 Answers 3

Reset to default 5

You 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