How to implement a countdown timer for the site so that the counter is reset every day?

  • The cool option is to use a cookie or localstorage. Give cookies for a day. If there is no picnic or if the time is up, we start the timer again - Mr. Black
  • And, perhaps, the label should be javascript - Mr. Black

2 answers 2

There are a lot of ambiguities in the question, for example, the simplest:

var t = Date.now() + 1000*60*60*24; // Надо сохранить if (Date.now() < t) { alert('Осталось: ' + (t - Date.now())); } else { alert('Время истекло'); } 
  • (missing) timer will show each site visitor something of their own? - Sergiks 2:29 pm
  • @Sergiks There is nothing about this in TK. - user208916 2:46 pm

If you understand correctly, you need to specify some one time of the day, to which the timer counts, and to which it is reset to 23:59:59

 // локальное время, // до которого таймер считает, // и когда сбрасывается var resetAt = '03:00'; // HH:MM resetAt = resetAt.split(':'); if( resetAt.length !== 2) throw "Bad time format"; resetAt = resetAt.map(function(e){return parseInt(e);}); var el = document.getElementById('out'); function countDown() { var D = new Date(); var h = resetAt[0] - D.getHours(); var m = resetAt[1] - D.getMinutes() - 1; var s = 60 - D.getSeconds() - 1; if( m < 0) { m += 60; h--; } if( h < 0) h += 24; h = ('0'+h).slice(-2); m = ('0'+m).slice(-2); s = ('0'+s).slice(-2); el.innerText = ''+ h+ ':'+ m+ ':'+ s; } countDown(); window.setInterval( countDown, 200); 
 <div id="out"></div> 

  • In my opinion this is called a clock. - user208916
  • @Khipster sandy. I understood the task of the vehicle like this. Do you have another idea? - Sergiks pm