I am currently trying to implement a system where a callback is triggered from within an event listener (code below). When I run the code, I get the error "callback is not a function", which I believe to be because the variable callback is not defined by the event listener.
function playSound(no, callback) {
var noOfSounds = sound().length;
if(no == "random" || no == undefined) {
act("There are "+noOfSounds+" Sounds");
var randomNo = Math.floor(Math.random() * noOfSounds);
act("Will use no."+randomNo);
playSound(randomNo);
} else {
sound()[no].addEventListener("ended", function() {
callback();
});
sound()[no].play();
}}
I think your problem is the way you are calling the function. If I look at line 10 you are saying
playSound(randomNo)
It should be
playSound(randomNo, function(){
//code to execute in callback
});
This is because you are calling trying to use callback which is a function and should be added to the input parameters when calling the method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.