如何使此音频文件仅在列出的时间播放,而不是之后的任何时间播放

How can i make this Audio file only play at the time listed and not any time after?

本文关键字:播放 时间 任何 之后 何使此 音频 文件      更新时间:2023-09-26

以下是我从这个网站得到的代码。谢谢。但是每次加载页面时,它都会在 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;

if (millisTill10 <0)

{ 毫秒蒂尔10 += 1000;现在是上午10点以后,明天上午10点试试。

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

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