Determining when it will be executed

Asked

Viewed 76 times

2

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 be executed when it reaches a certain time in my code, without the user having to update the page.

How can I do that?

2 answers

3

  • Hello, @bigown I tried to do so : https://codeshare.io/LQUun . But once I refresh the page, Alert is already displayed.... What could be?

  • 1

    I had forgotten to take the current time.

3


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)
}

https://jsfiddle.net/xpns7cup/

  • 1

    That’s exactly what it was, thank you very much!

  • 1

    I’m glad I helped @Naldson, I hope you’ve studied the code to understand how it works. Studying small details makes you a great programmer! Abrs and good studies!

  • 1

    Thank you very much, @Willy

Browser other questions tagged

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