0

以下是我从该站点获得的代码。谢谢你。但每次加载页面时,它都会在 16:24 之后的任何时间播放音频文件。有没有办法防止这种情况?

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;

如果 (millisTill10 < 0)

{millisTill10 += 1000; // 现在是上午 10 点之后,明天上午 10 点试试。

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

4

1 回答 1

0
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();
}
于 2016-02-01T22:01:05.970 回答