With Javascript, I have a function that creates an audio element with createElement("audio")
, and start playing in loop without using appendChild()
, I mean without appending it to the DOM.
The element created is kept in a variable, let s called it music1
:
music = document.createElement("audio");
music.addEventListener("loadeddata", function() {
music.play();
});
music.setAttribute("src", music_Source);
What I would like to do, is to change the music played, if possible using the same function, and storing the element in the same variable.
我这样做的是,在以上法典之前:
if(typeof(music) == "object") {
music.pause();
music = null;
}
但是,如果我删除
Do you have any idea to properly remove the first music, delete the element, or so on?
实际上,肯尼斯评论是正确的,我试图只是改变rc属性,而不是修改“肌肉”变数(要么将其定为无效,要么重新定性为`工具'),似乎也做了工作。 因此,记录: 此处变化的每个来源的职能是:
if(typeof(music) != "object") {
//audio element does not exist yet:
music = document.createElement("audio");
music.addEventListener("loadeddata", function() {
music.play();
});
}
music.setAttribute("src", music_Source);