创建函数以根据 Javascript 中的变量列表创建函数
Creating a function to create a function from list of variables in Javascript
抱歉,如果标题跑题了,我可能想不出该怎么说了。
我正在制作一个 chrome 网络应用程序,它是一种音板。现在定义音频并听一个按钮点击我一直这样做
//Variables for creating audio loads//
var heyboosong = new Audio();
var ricksong = new Audio();
var rapgod = new Audio();
//stopfunction has a button on the html that goes and will pause the sound playing. right now it's a list of every sound. I need to change it to just pause anything that plays//
function stopmusic() {
heyboosong.pause();
ricksong.pause();
rapgod.pause();
//audio functions to listen to the buttons and make sure they are clicked, if so then wohoo sound//
function myboo() {
heyboosong.src = "mp3/myboo.mp3";
heyboosong.play();
}
document.getElementById('myboobutton').addEventListener('click', myboo);
function rickplay() {
ricksong.src = "mp3/rick.mp3";
ricksong.play();
}
document.getElementById('rickbutton').addEventListener('click', rickplay);
function rapgodplay() {
rapgod.src = "mp3/rapgod.mp3";
rapgod.play();
}
现在这一切都很好而且花花公子,但是这很痛苦。我有超过 100 种不同的声音,而不仅仅是这三种。所以我每次都必须把所有的东西都打出来。无论如何我可以用 Jquery 或其他方法压缩这个?
我不知道它是否有效,但它会给你一些想法。如果能把歌名存到变量里,我想就可以了。
var mySong = new Audio();
function stopmusic() {
mySong.pause();
}
function getSong(title) {
mySong.src = "mp3/"+ title +".mp3";
mySong.play();
}
document.getElementById(title+'button').addEventListener('click', title);
抱歉,如果标题跑题了,我可能想不出该怎么说了。
我正在制作一个 chrome 网络应用程序,它是一种音板。现在定义音频并听一个按钮点击我一直这样做
//Variables for creating audio loads//
var heyboosong = new Audio();
var ricksong = new Audio();
var rapgod = new Audio();
//stopfunction has a button on the html that goes and will pause the sound playing. right now it's a list of every sound. I need to change it to just pause anything that plays//
function stopmusic() {
heyboosong.pause();
ricksong.pause();
rapgod.pause();
//audio functions to listen to the buttons and make sure they are clicked, if so then wohoo sound//
function myboo() {
heyboosong.src = "mp3/myboo.mp3";
heyboosong.play();
}
document.getElementById('myboobutton').addEventListener('click', myboo);
function rickplay() {
ricksong.src = "mp3/rick.mp3";
ricksong.play();
}
document.getElementById('rickbutton').addEventListener('click', rickplay);
function rapgodplay() {
rapgod.src = "mp3/rapgod.mp3";
rapgod.play();
}
现在这一切都很好而且花花公子,但是这很痛苦。我有超过 100 种不同的声音,而不仅仅是这三种。所以我每次都必须把所有的东西都打出来。无论如何我可以用 Jquery 或其他方法压缩这个?
我不知道它是否有效,但它会给你一些想法。如果能把歌名存到变量里,我想就可以了。
var mySong = new Audio();
function stopmusic() {
mySong.pause();
}
function getSong(title) {
mySong.src = "mp3/"+ title +".mp3";
mySong.play();
}
document.getElementById(title+'button').addEventListener('click', title);