Javascript 当它应该是定时事件时,在加载时播放音频声音
音频设置为在特定时间播放,就像我主页背景中内置的闹钟一样。它在正确的时间播放。然而,出于某种原因,它也在加载网页时播放。如果有人能弄明白这一点,我将不胜感激,因为我是个笨蛋Javascript 当它应该是定时事件时,在加载时播放音频声音,javascript,Javascript,音频设置为在特定时间播放,就像我主页背景中内置的闹钟一样。它在正确的时间播放。然而,出于某种原因,它也在加载网页时播放。如果有人能弄明白这一点,我将不胜感激,因为我是个笨蛋 var date = new Date(), year = date.getFullYear(), month = date.getMonth(), weekday = date.getDay(), day = date.getDate(), time = date.getTime()
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);
var date=新日期(),
年份=日期。getFullYear(),
月=日期。getMonth(),
weekday=date.getDay(),
day=date.getDate(),
time=date.getTime(),
timeout1=新日期(年、月、日、12、15、0、0),
timeout2=新日期(年、月、日、14、30、0、0),
timeout3=新日期(年、月、日、17、0、0、0)。getTime()-时间,
timeout4=新日期(年、月、日、19、0、0、0),
timeout5=新日期(年、月、日、23、45、0、0),
mp3=新音频(“Audio/alarm.mp3”),
ogg=新音频(“Audio/alarm.ogg”),
音频
如果(mp3.canPlayType的类型==“函数”&&mp3.canPlayType(“音频/mpeg”)!==“”)
音频=mp3;
else if(ogg.canPlayType的类型==“函数”&&ogg.canPlayType(“音频/ogg”)!==“”)
音频=ogg;
setTimeout(函数(){
如果(工作日>0&&工作日<6){
音频播放();
}
},超时1);
setTimeout(函数(){
如果(工作日>0&&工作日<6){
音频播放();
}
},超时2);
setTimeout(函数(){
如果(工作日>0&&工作日<6){
音频播放();
}
},超时3);
setTimeout(函数(){
如果(工作日>0&&工作日<6){
音频播放();
}
},超时4);
setTimeout(函数(){
如果(工作日>0&&工作日<6){
音频播放();
}
},超时5);
超时1是负值,因为它在中午12点之后。至少在我的时区EST。因此,您可能应该为正时间添加一个条件
您只需将超时封装在if子句中,如:
if(timout1 > 0){
setTimeout(function(){
if (weekday > 0 && weekday < 6) {
audio.play();
}
}, timeout1);
}
if(timout1>0){
setTimeout(函数(){
如果(工作日>0&&工作日<6){
音频播放();
}
},超时1);
}
另外,我会重新构造它,以限制您在每次超时时重写所有内容
var date = new Date(),
year = date.getFullYear(),
month = date.getMonth(),
weekday = date.getDay(),
day = date.getDate(),
time = date.getTime(),
timeouts = [],
timeouts.push(new Date(year, month, day, 12, 15, 0, 0).getTime() - time),
timeouts.push(new Date(year, month, day, 14, 30, 0, 0).getTime() - time),
timeouts.push(new Date(year, month, day, 17, 0, 0, 0).getTime() - time),
timeouts.push(new Date(year, month, day, 19, 0, 0, 0).getTime() - time),
timeouts.push(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;
for(var i=0;i<timeouts.length;i++){
if(timeouts[i] > 0){
setTimeout(function(){
audio.play();
}, timeouts[i]);
}
}
var date=新日期(),
年份=日期。getFullYear(),
月=日期。getMonth(),
weekday=date.getDay(),
day=date.getDate(),
time=date.getTime(),
超时=[],
push(新日期(年、月、日、12、15、0、0).getTime()-time),
push(新日期(年、月、日、14、30、0、0).getTime()-time),
push(新日期(年、月、日、17、0、0、0).getTime()-time),
push(新日期(年、月、日、19、0、0、0).getTime()-time),
push(新日期(年、月、日、23、45、0、0).getTime()-time),
mp3=新音频(“Audio/alarm.mp3”),
ogg=新音频(“Audio/alarm.ogg”),
音频
如果(mp3.canPlayType的类型==“函数”&&mp3.canPlayType(“音频/mpeg”)!==“”)
音频=mp3;
else if(ogg.canPlayType的类型==“函数”&&ogg.canPlayType(“音频/ogg”)!==“”)
音频=ogg;
对于(变量i=0;i 0){
setTimeout(函数(){
音频播放();
},超时[i]);
}
}
编辑:更正了由于打字错误造成的错误。超时1是负值,因为它在中午12点之后。至少在我的时区EST。因此,您可能应该为正时间添加一个条件 您只需将超时封装在if子句中,如:
if(timout1 > 0){
setTimeout(function(){
if (weekday > 0 && weekday < 6) {
audio.play();
}
}, timeout1);
}
if(timout1>0){
setTimeout(函数(){
如果(工作日>0&&工作日<6){
音频播放();
}
},超时1);
}
另外,我会重新构造它,以限制您在每次超时时重写所有内容
var date = new Date(),
year = date.getFullYear(),
month = date.getMonth(),
weekday = date.getDay(),
day = date.getDate(),
time = date.getTime(),
timeouts = [],
timeouts.push(new Date(year, month, day, 12, 15, 0, 0).getTime() - time),
timeouts.push(new Date(year, month, day, 14, 30, 0, 0).getTime() - time),
timeouts.push(new Date(year, month, day, 17, 0, 0, 0).getTime() - time),
timeouts.push(new Date(year, month, day, 19, 0, 0, 0).getTime() - time),
timeouts.push(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;
for(var i=0;i<timeouts.length;i++){
if(timeouts[i] > 0){
setTimeout(function(){
audio.play();
}, timeouts[i]);
}
}
var date=新日期(),
年份=日期。getFullYear(),
月=日期。getMonth(),
weekday=date.getDay(),
day=date.getDate(),
time=date.getTime(),
超时=[],
push(新日期(年、月、日、12、15、0、0).getTime()-time),
push(新日期(年、月、日、14、30、0、0).getTime()-time),
push(新日期(年、月、日、17、0、0、0).getTime()-time),
push(新日期(年、月、日、19、0、0、0).getTime()-time),
push(新日期(年、月、日、23、45、0、0).getTime()-time),
mp3=新音频(“Audio/alarm.mp3”),
ogg=新音频(“Audio/alarm.ogg”),
音频
如果(mp3.canPlayType的类型==“函数”&&mp3.canPlayType(“音频/mpeg”)!==“”)
音频=mp3;
else if(ogg.canPlayType的类型==“函数”&&ogg.canPlayType(“音频/ogg”)!==“”)
音频=ogg;
对于(变量i=0;i 0){
setTimeout(函数(){
音频播放();
},超时[i]);
}
}
编辑:更正了由于输入错误而导致的错误。在每个超时中添加一个
console.log()
,查看哪一个正在播放肯定是第一个,因此JasonMiraglia到目前为止是正确的,只是想知道如何使它成为一个积极的时间添加一个console.log()
在您每次暂停时,查看哪一个正在播放肯定是第一个,因此JasonMiraglia到目前为止是正确的,只是想知道如何使它成为一个积极的时间感谢您的支持,帮了我很多。感谢您的支持,帮了我很多。