gpt4 book ai didi

javascript - 避免 Javascript 中的重复代码?

转载 作者:行者123 更新时间:2023-11-27 23:25:45 24 4
gpt4 key购买 nike

这次我正在从事一个涉及每小时播放音乐的元素,在 html 页面上播放整整一个小时。事情是。 . .每小时播放一首不同的歌曲。是的。这意味着我有 24 个不同的 <audio>我的 html 中的标签(我对此很好,假设没有办法使用更少的代码。)。现在,事情变得棘手的部分是在 JavaScript 中。这是我的代码,可以让每首歌每小时播放一次。

   var curHour = new Date().getHours(); //finds the current hour
//grabs the music
var music1 = document.getElementById('music1');
var music2 = document.getElementById('music2');
var music3 = document.getElementById('music3');
var music4 = document.getElementById('music4');
var music5 = document.getElementById('music5');
var music6 = document.getElementById('music6');
var music7 = document.getElementById('music7');
var music8 = document.getElementById('music8');
var music9 = document.getElementById('music9');
var music10 = document.getElementById('music10');
var music11 = document.getElementById('music11');
var music12 = document.getElementById('music12');
//etc. etc. etc.

//tells the music when to play
if (curHour === 1) {
music1.play();
}
else if (curHour === 2) {
music2.play();
}
else if (curHour === 3) {
music3.play();
}
//etc. etc. etc.

如您所见,这段代码似乎太重复了!为了“清理”代码,我尝试了 for()循环,尽管我只完成了一半就意识到我这样做的方式会使告诉一首歌(以变量的形式)播放不可能。我也尝试了一些类似的东西:

var grab = document.getElementById;
var hourMusic = [grab('music1'), grab('music2')];
if (curHour === 10) {
hourMusic[0].play()
}
else {
hourMusic[1].play()
}

这没有用。 . .而且我看得越多,我认为这会起作用似乎有点愚蠢。但是,嘿,至少我可以说我试过了(即使您可能无法将 document.getElementById 本身保存在变量中)。

最佳答案

我建议你只创建一个 <audio>元素并覆盖它的 src改变歌曲的属性,而不是创建一首 <audio>一天中每个小时的元素。

在页面加载时,您可以调用一个函数来检索当前时间和该时间对应的歌曲 URL,设置 src单数的属性 <audio>元素到该 URL,然后播放它。您还可以在播放结束时使用 onended 调用该函数事件实现循环播放,另外在整点时改变歌曲。

// note: I was lazy and just repeated 3 URLs 8 times
var songDir = 'http://www.stephaniequinn.com/Music/';
var songURLs = [
songDir+'Commercial%20DEMO%20-%2001.mp3',
songDir+'Commercial%20DEMO%20-%2002.mp3',
songDir+'Bach%20-%20Jesu,%20Joy%20of%20Man\'s%20Desiring.mp3',
songDir+'Commercial%20DEMO%20-%2001.mp3',
songDir+'Commercial%20DEMO%20-%2002.mp3',
songDir+'Bach%20-%20Jesu,%20Joy%20of%20Man\'s%20Desiring.mp3',
songDir+'Commercial%20DEMO%20-%2001.mp3',
songDir+'Commercial%20DEMO%20-%2002.mp3',
songDir+'Bach%20-%20Jesu,%20Joy%20of%20Man\'s%20Desiring.mp3',
songDir+'Commercial%20DEMO%20-%2001.mp3',
songDir+'Commercial%20DEMO%20-%2002.mp3',
songDir+'Bach%20-%20Jesu,%20Joy%20of%20Man\'s%20Desiring.mp3',
songDir+'Commercial%20DEMO%20-%2001.mp3',
songDir+'Commercial%20DEMO%20-%2002.mp3',
songDir+'Bach%20-%20Jesu,%20Joy%20of%20Man\'s%20Desiring.mp3',
songDir+'Commercial%20DEMO%20-%2001.mp3',
songDir+'Commercial%20DEMO%20-%2002.mp3',
songDir+'Bach%20-%20Jesu,%20Joy%20of%20Man\'s%20Desiring.mp3',
songDir+'Commercial%20DEMO%20-%2001.mp3',
songDir+'Commercial%20DEMO%20-%2002.mp3',
songDir+'Bach%20-%20Jesu,%20Joy%20of%20Man\'s%20Desiring.mp3',
songDir+'Commercial%20DEMO%20-%2001.mp3',
songDir+'Commercial%20DEMO%20-%2002.mp3',
songDir+'Bach%20-%20Jesu,%20Joy%20of%20Man\'s%20Desiring.mp3',
];

window.playSongByHour = function(hour) {
let audioElem = document.getElementById('audio1');
audioElem.setAttribute('src',songURLs[hour]); // hour must be in 0:23
audioElem.play();
}; // end function()
window.playSongByCurrentHour = function() { playSongByHour(new Date().getHours()); };
<body onload="playSongByCurrentHour();">
<audio id="audio1" controls="1" onended="playSongByCurrentHour();"/>
</body>

关于javascript - 避免 Javascript 中的重复代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38690855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com