Determining the time at which function to execute

1

Example:

var horaAtual = new Date();

var horaInicio = new Date("Fri Apr 01 2016 23:30:00");

//Quando(horaAtual == horaInicio)
//execute algo...

I want a function to run when it reaches an hour already set in my code, without the user needing to refresh the page.

How can I do this?

    
asked by anonymous 02.04.2016 / 04:22

2 answers

2

Try this:

var specificTime = new Date('Fri Apr 01 2016 23:39:49').getTime();
var nowTime = new Date().getTime();

if (specificTime > nowTime) {
    window.setTimeout(function () {
    alert('It is time!')
  }, specificTime - nowTime)
}

link

    
02.04.2016 / 04:45
2

The function that does this is setTimeout () .

var horaInicio = new Date("Fri Apr 01 2016 23:55:00") - new Date();
setTimeout(function(){ alert("teste" )}, horaInicio);
    
02.04.2016 / 04:30