0

I would like to update a greeting message at 6am, 12pm, and 6pm. I am looking for the most efficient way without polling the current time too frequently inside a loop.

setInterval(function(){
    var now = new Date();
    if(now.getHours() == 6 && now.getMinutes() == 0 && now.getSeconds() == 0){
        document.getElementById('greeting').innerHTML = "Good Morning";
    } else if (now.getHours() == 12 && now.getMinutes() == 0 && now.getSeconds() == 0){
        document.getElementById('greeting').innerHTML = "Good Afternoon";
    } else if (now.getHours() == 18 && now.getMinutes() == 0 && now.getSeconds() == 0){
        document.getElementById('greeting').innerHTML = "Good Evening";
    }   
},3600000);

My immediate solution, shown above, was to poll the current time every hour. As you can imagine, there is a lot of wasted processing here to just update something 3 times in a 24 hour period. Furthermore, what if the page loads in the middle of the hour, then it will miss the update.

5
  • 2
    there is a lot of wasted processing here Where's this extra processing? An extremely small amount of code runs every hour? Is this slowing down your site somehow? For the rest, just stop checking minutes and seconds... Commented Sep 1, 2016 at 18:13
  • @MikeMcCaughan so you are saying this is the best solution (minus the checks for minutes and seconds)? Commented Sep 1, 2016 at 18:15
  • 1
    Check for hour ranges rather than an absolute value, and yeah no need to check for minutes or seconds. Also run the code when the page loads (create a function that does this and reference the function in your setInterval call but also on page load).
    – Jasper
    Commented Sep 1, 2016 at 18:15
  • There's this idea of "premature optimization" which you might be interested in :). Basically, it boils down to "if it ain't broke, don't fix it". You can add features like those mentioned by @Jasper and t.niese, but really, what you have (checking the hour only) is "good enough". Commented Sep 1, 2016 at 18:20
  • I wouldn't worry about "too much processing". Hardware can handle thousands of processes a minute. I don't think this will be a problem :) Commented Sep 1, 2016 at 18:22

1 Answer 1

2

Your code could look something like that:

function updateTime() {
  var now = new Date();

  //display the greeting message base on the hour range
  if (now.getHours() >= 6 && now.getHours() < 12) {
    document.getElementById('greeting').innerHTML = "Good Morning";
  } else if (now.getHours() > 12 && now.getHours() < 18) {
    document.getElementById('greeting').innerHTML = "Good Afternoon";
  } else if (now.getHours() >= 18 || now.getHours() < 6) {
    document.getElementById('greeting').innerHTML = "Good Evening";
  }

  //do the next check to the next full hour and 1 minute
  setTimeout(updateTime, (60 - now.getMinutes() + 1) * 60 * 1000);
}


updateTime();

The used cpu load that happens every hour is negligible.

1
  • setTimeout takes milliseconds, so 60 - 49 (49 minutes after the hour) * 60 (to get seconds) * 1000 (to get milliseconds) in order to find how many milliseconds until the next full hour. Commented Jun 21, 2017 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.