gpt4 book ai didi

Javascript 对象函数调用不起作用

转载 作者:行者123 更新时间:2023-11-28 05:38:59 25 4
gpt4 key购买 nike

我正在开发 HTML5 音板,但遇到了一些障碍......

我正在尝试获得一个停止功能来同时处理所有播放的声音。不幸的是,当我通过按下按钮调用此函数时,该对象似乎没有停止函数。实际声音元素的代码如下:

// Container to keep all sounds in one place. This is a Dictionary within a dictionary to be able to search by catagory.
var sounds = {};

// Counter to keep track of unique ID's
var lastID = 0;


// Base class for initializing Any class
var Base = function(methods){
var base = function() {
this.initialize.apply(this, arguments);
};

for (var property in methods) {
base.prototype[property] = methods[property];
}

if (!base.prototype.initialize) base.prototype.initialize = function(){};

return base;
};

//Complete class for the Sound object. Generates its own DIV's and HTML5 tags to play stuff.
var Sound = Base({
// Init all the variables.
initialize: function(name, file, target='Sounds') {
this.name = name;
this.file = file
this.button = null;
this.audioelement;
this.id = lastID + 1;
this.target = target;
lastID ++;


// Check if the catagory is there, if not: create it with a placeholder object
var catagory = sounds[this.target];
if(catagory == null){
sounds[this.target] = {99:null};

}
sounds[this.target][this.id] = this;

// Call init function
this.init();
},

play : function() {
obj = this
if(obj.audioelement.paused == true){
obj.audioelement.play();
}else{
obj.audioelement.pause();
obj.audioelement.fastSeek(0);
}

},
stop : function(){
obj = this;
obj.audioelement.pause();
},
init : function(){

// Statement for JS class based shenanigans.
obj = this

// Create a button and add some text to it
obj.button = document.createElement("BUTTON");
obj.button.appendChild(document.createTextNode(obj.name));

// Set ID's and names to keep track of this button
obj.button.id = obj.id;
obj.button.name = obj.target;

// Get or create parent element. Used for catagory based display
var el = getOrCreateElement(obj.target)
el.appendChild(obj.button);

// Create audio element and set appropriate settings
obj.audioelement = document.createElement("AUDIO");
obj.audioelement.src = obj.file;
obj.audioelement.name
obj.button.appendChild(obj.audioelement);

// Add function to play/pause to button
obj.button.onclick = buttonClicked;

});
function buttonClicked(){
// Fetch sound from dicionary container using the name and id from the button [SET AT SOUND.INIT()]
var sound = sounds[this.name][this.id];
// Call the play function in [SOUND]
sound.play();
}

对于 stopall 函数:

function stopAll(){
// Scroll through the entire dictionary
for ( var key in sounds){
for ( var id in sounds[key]){
// Check if the sound is not a placeholder
if(id == 99){
continue;
}
// Call stop function with fetched object.
var sound = sounds[key][id];
sound.stop();
}
}
}

奇怪的是,播放功能似乎有效,但停止功能不起作用。它表示该对象没有该特定功能...

任何想法都会受到赞赏!

WM

最佳答案

for ( var x in object) 除了方法之外,还会循环访问更多属性,包括基础对象原型(prototype)上的属性。

如果您在该内部循环中console.log(id);,您将看到额外的内容。

尝试将其添加到内部 for 循环中:

if (typeof id !== 'function') {
continue;
}

编辑:这不太正确,但我现在无法看到它应该是什么。凭内存,就快到了!继续玩它。

关于Javascript 对象函数调用不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39089497/

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