0
votes

Below is the code I got from this site. THANK YOU. but everytime the page is loaded it plays the audio file any time after 16:24. Is there a way to prevent this?

var now = new Date(); var audio1 = new Audio('one_minute_remaining-2.mp3');

var millisTill10 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 16, 24, 00, 0) -now;

if (millisTill10 < 0)

{ millisTill10 += 1000; // it's after 10am, try 10am tomorrow.

} setTimeout(function(){audio1.play()}, millisTill10);

1

1 Answers

0
votes
var now = new Date(); 
var audio1 = new Audio('one_minute_remaining-2.mp3');

//Change the hours, minutes, seconds to the time you want it to play
var timeIWantItToPlay = new Date( 
    now.getFullYear(), 
    now.getMonth(), 
    now.getDate(),
    16, 24, 00, 0
);

//This is the "exactness" of the time. So if it's within 1 second, it will play
var leeway = 1000;

if ( ( now.getTime() > timeIWantItToPlay.getTime() - leeway ) && ( now.getTime() < timeIWantItToPlay.getTime() + leeway )
{
     audio1.play();
}