Doubts with timer

1

I have the following doubt, I have a countdown timer, and for example, I click the button, the button becomes invisible and the timer starts, my problem is that when I change the page and return it the button is active and I only want the active button when the timer runs out.

I'm using setTimeout

 setTimeout(() => {
            var disableButton1 = document.getElementById("startMission1")
            var disableButton2 = document.getElementById("startMission2")
            var disableButton3 = document.getElementById("startMission3")

            disableButton1.style.display='block'
            disableButton2.style.display='block'
            disableButton3.style.display='block'

            disableButton4.style.display='block'
            disableButton5.style.display='block'
            disableButton6.style.display='block'

            div1.style.marginTop= "-20%"

            var currentUser = Meteor.user();
            var total = (Number(currentUser.profile.hp) + Number(currentUser.profile.attack) + Number(currentUser.profile.luck));
            var actualPoints = (Number(currentUser.profile.points))
            var totalPoints = (total + actualPoints)

            Meteor.users.update({_id: Meteor.userId()},{$set:{"profile.points": totalPoints}})
        }, 1200000) 
    
asked by anonymous 22.05.2018 / 11:05

1 answer

0

You can also use the Meteor Session package to create a reactive session variable.

meteor add session

Template HTML

<button type="button" name="button" class="{{ClassDoBotao}}"></button>

Template JS

/*onCreate*/
Meteor.setInterval(function(){
  Session.set("now", moment().valueOf()); //timestamp de agora
}, 1000/60);

/*Helper*/
ClassDoBotao(){
  var now = Session.get("now"); //torna se reativo
  var inativo_ate = Session.get("inativo_ate"); //torna se reativo
  if(inativo_ate && now >= inativo_ate){
    Session.clear("inativo_ate"); //limpa a variavel de sessao
    return "ativo";
  }
  return "inativo";
}

/*on click event*/
(evt){
  //conta 2 minutos
  Session.set("inativo_ate", moment().add(2, "minutes").valueOf());
}
    
06.08.2018 / 21:35