increment event after a certain time

Asked

Viewed 71 times

1

I am making an application that simulates a point marker, and I would need it to increase the next mark only after 5 minutes, I did some tests with setTimeout, but I didn’t get what I needed. The application makes the appointments, but does not have the time timer(5 minutes).

js:

    var qtd = document.getElementById("dataTime");

function getTime(){

  var data = new Date();
  var full_time = data.getHours() + ":" + data.getMinutes() + ":" + data.getSeconds();

  var dataCompleta = full_time;

  while (qtd < 4){
   document.getElementById("horaMarcada").insertAdjacentHTML('beforeend', '<span id="dataTime">'+ dataCompleta +' </span>');
   qtd++;
   return false;
  }
}

html:

<button onclick="getTime()">Marcar</button>
  • it would not be better if you save the time in the database and go checking the difference with the setTimeout? so it would be more reliable and safer for your application.

  • at first I want to save the tags with localStorage

  • You want 3 marks or 4?

  • 4 appointments, the first in the act and the other 3 only after 5 minutes, if the person clicks before 5 minutes I will present a message saying that only after 5 minutes

  • Obs: and the other 3, each only after 5 minutes

1 answer

2


It is necessary to change the <span id="dataTime"> for <span class="dataTime"> because elements are being created with the same id, what is already wrong.

With class you can count the amount of elements in real time, dispensing the self-improvement with qtd++.

By clicking the button, the attribute onclick changes to a alert, and after 5 minutes, back to normal.

You can put everything within the function in the order below:

function getTime(){

   document.querySelector("button").setAttribute("onclick","alert('Aguarde 5 minutos!')");
   
   var data = new Date();
   var full_time = data.getHours() + ":" + data.getMinutes() + ":" + data.getSeconds();
   var dataCompleta = full_time;
   
   document.getElementById("horaMarcada").insertAdjacentHTML('beforeend', '<span class="dataTime">'+ dataCompleta +' </span>');
   var qtd = document.getElementsByClassName("dataTime").length;

   if(qtd < 4){
      setTimeout(function(){
         document.querySelector("button").setAttribute("onclick","getTime()");
      }, 300000);
   }else{
      document.querySelector("button").disabled = true;
      console.log("fim");
   }
}
<button onclick="getTime()">Marcar</button>
<div id="horaMarcada"></div>

  • DVD, I ran a test on a separate file, but it’s not rolling the timer

  • you are doing the loop, but do not pause 5 minutes for the next dial, I changed to 300000

  • dvd, I’m using the same, I created a Jsfiddle http://jsfiddle.net/josecarlosdw/0ndv7hpp/

  • dvd, see my result https://imgur.com/ZObAdNs

  • @josecarlos Now I think it was. Abs!

  • dvd, exactly that, thank you very much! abs

  • @josecarlos make sure to mark the answer as right. Abs!

  • Marked, my dear. abs!

Show 3 more comments

Browser other questions tagged

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