【问题标题】:How can i make this Audio file only play at the time listed and not any time after?我怎样才能让这个音频文件只在列出的时间播放,而不是之后的任何时间?
【发布时间】:2016-05-10 12:15:08
【问题描述】:

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

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

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

【问题讨论】:

    标签: javascript audio timed


    【解决方案1】:
    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();
    }
    

    【讨论】:

    • 我试过了,但是声音没有在设定的时间播放。
    猜你喜欢
    • 2013-05-21
    • 2014-10-16
    • 2021-07-25
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 2017-02-02
    相关资源
    最近更新 更多