音频设置为在特定时间播放,就像我主页背景中内置的闹钟一样。它在正确的时间播放。但是,由于某种原因,它也在加载网页时播放。如果有人能弄清楚那将非常感激,因为我一无所知。
var date = new Date(),
year = date.getFullYear(),
month = date.getMonth(),
weekday = date.getDay(),
day = date.getDate(),
time = date.getTime(),
timeout1 = new Date(year, month, day, 12, 15, 0, 0).getTime() - time,
timeout2 = new Date(year, month, day, 14, 30, 0, 0).getTime() - time,
timeout3 = new Date(year, month, day, 17, 0, 0, 0).getTime() - time,
timeout4 = new Date(year, month, day, 19, 0, 0, 0).getTime() - time,
timeout5 = new Date(year, month, day, 23, 45, 0, 0).getTime() - time,
mp3 = new Audio("audio/alarm.mp3"),
ogg = new Audio("audio/alarm.ogg"),
audio;
if (typeof mp3.canPlayType === "function" && mp3.canPlayType("audio/mpeg") !== "")
audio = mp3;
else if (typeof ogg.canPlayType === "function" && ogg.canPlayType("audio/ogg") !== "")
audio = ogg;
setTimeout(function(){
if (weekday > 0 && weekday < 6) {
audio.play();
}
}, timeout1);
setTimeout(function(){
if (weekday > 0 && weekday < 6) {
audio.play();
}
}, timeout2);
setTimeout(function(){
if (weekday > 0 && weekday < 6) {
audio.play();
}
}, timeout3);
setTimeout(function(){
if (weekday > 0 && weekday < 6) {
audio.play();
}
}, timeout4);
setTimeout(function(){
if (weekday > 0 && weekday < 6) {
audio.play();
}
}, timeout5);