0
(function() {
    var date = new Date();
    if ( (date.getHours() >= 17 && date.getMinutes() >= 00) || (date.getHours() <= 20 && date.getMinutes() <= 30) ) {
        var timer = setInterval(function() {
            $('.output').html( date.toLocaleTimeString() );  
        }, 1000);
    } else {
        clearInterval(timer);
    }
})();

Something I'm confused. How to make a specific time displays the current time?

Aleksandr
  • 439
  • 3
  • 13

1 Answers1

1

You need to evaluate your date on every iteration of the interval.

(function() {
  var date;
  var timer = setInterval(function() {
    date = new Date();
    if ( date.getHours() >= 17 && date.getHours() < 20 ) ) {
      $('.output').html( date.toLocaleTimeString() );  
    } else {
      clearInterval(timer);
    } 
  }, 1000);
})();
Dane O'Connor
  • 75,180
  • 37
  • 119
  • 173
  • clearInterval does not work – Aleksandr Jan 12 '15 at 00:44
  • @AleksandrAleksandrov Yes it does. See this simplified example: http://jsfiddle.net/41obcvys/1/ It prints 2 console messages then stops. Your conditional might not be testing what you think it is. – Dane O'Connor Jan 12 '15 at 00:51
  • I understand your example. But these dates confuses me, I can not set the condition. – Aleksandr Jan 12 '15 at 01:08
  • Ask another question which is more specific about the date condition (link if you'd like). This question was about updating something on an interval. You haven't given enough information for me to help you with the conditional (so another question would help). – Dane O'Connor Jan 12 '15 at 01:20
  • I need to print only the date from 17 to 20 hours. – Aleksandr Jan 12 '15 at 01:28