Counter reset every time reset

Asked

Viewed 302 times

3

I have a contact day, hour, minute and second, from there I program for example: in 2 days (regressive), I would like when it zeroes start counting again, type 2 in 2 days, or 1 in 1, not to need every time Zera have to keep making date changes!
Follow the code:

var target_date = new Date("\n" + " October 15, 2019").getTime();
var dias, horas, minutos, segundos;
var regressiva = document.getElementById("regressiva");

setInterval(function() {

    var current_date = new Date().getTime();
    var segundos_f = (target_date - current_date) / 1000;

    dias = parseInt(segundos_f / 86400);
    segundos_f = segundos_f % 86400;

    horas = parseInt(segundos_f / 3600);
    segundos_f = segundos_f % 3600;

    minutos = parseInt(segundos_f / 60);
    segundos = parseInt(segundos_f % 60);


    document.getElementById('dia').innerHTML = dias;
    document.getElementById('hora').innerHTML = horas;
    document.getElementById('minuto').innerHTML = minutos;
    document.getElementById('segundo').innerHTML = segundos;


}, 1000);

2 answers

2

Paulo, check that the timer has reached zero and then assign a new value to target_date. This way whenever the timer zeroes will be assigned a new timer of x days more:

// (...)
var segundos_f = (target_date - current_date) / 1000;
if (segundos_f <= 0) {
    const a = new Date();
    a.setDate(a.getDate() + 2 /* mais 2 dias, por exemplo */);
    target_date = a.getTime();
    return;
}
// (...)
  • then when Zera it starts -1 -1 -1 -1 type like this, starts negatively and goes!

  • when I update the page it comes back from the beginning

  • @Paulojunior every time the page is (re)loaded the script is interpreted again. To save keeping information between page exchanges use some mechanism such as localStorage, sessionStorage, cookies, Session, etc..

1

Hello Paul I made an example, but, I did not test. But the logic I implemented is very simple:

1. I put the day on the date in the form of string template to make it easier to manipulate the counting days.

2. I removed the codes from inside the setInterval() and inserted within a function, more for the sake of readability.

3. I created a if within the function for when hours, minutes and seconds are zeros increment the date.

4. Also by insertion legibility 0 before the time, minutes and seconds less than 10.

var data = 15;
var target_date = new Date(`\n October ${data}, 2019`).getTime();
var dias, horas, minutos, segundos;
var regressiva = document.getElementById("regressiva");

setInterval(function() {
  Timer();
}, 1000);

function Timer() {
  var current_date = new Date().getTime();
  var segundos_f = (target_date - current_date) / 1000;

  dias = parseInt(segundos_f / 86400);
  segundos_f = segundos_f % 86400;

  horas = parseInt(segundos_f / 3600);
  segundos_f = segundos_f % 3600;

  minutos = parseInt(segundos_f / 60);
  segundos = parseInt(segundos_f % 60);

  document.getElementById('dia').innerHTML = dias;
  document.getElementById('hora').innerHTML = horas < 10 ? `0${horas}` : `${horas}`;
  document.getElementById('minuto').innerHTML = minutos < 10 ? `0${minutos}` : `${minutos}`;
  document.getElementById('segundo').innerHTML = segundos < 10 ? `0${segundos}` : `${segundos}`;
  
  if(horas == 0 && minutos == 0 && segundos == 0) {
    data = data + 2;
  }
}
<b>Dias</b> <span id="dia"></span> -
<b>Tempo</b> <span id="hora"></span>:<span id="minuto"></span>:<span id="segundo"></span>

  • Guy’s less than a day away, he gets fucked up

  • Like you’re testing, you get fucked up like?

  • like when Zera it starts progressive counting and looks like this: 0:0-12:0-12:0-15

  • So Paul the problem is that it’s hard to simulate the problem, probably these accounts of % and division by 1000 interfere in the flow of the count in the function. But since I couldn’t test it, I won’t be able to tell you for sure.

  • So then you would have a regressive chronometer like this, which is done this way, Zera it adds 1 or 2 days!

  • He saw Paul as he is testing, for here I simulated and worked perfectly. With my code I put 12 in the date variable and I set the clock of the micro to 11:58, when arriving at 00:00 he added 2 days in the variable days and resumed the countdown normally.

  • Here it did not work no, it zeroed from there arrived began to count negative

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.