0

I program with the spotify API.. the error are these 2 functions, I dont know how I call the 'setVolume' function in this arrow-function. Is this possible or is the Code wrong?

I don't know any further. Here is my code:

player.on('player_state_changed', state => {

   //... SOME CODE ...

   

   function setVolume(value) {
       player.setVolume(value).then(() => {
           console.log('Volume updated!');
       });
   }

});

(1) In the console I tried this:

setVolume(0.5)

(2) In the console I tried this:

player.on(setVolume(0.5))

~ and I got this error message: "player is not defined"

So how can I call the setVolume function from the console?

7
  • You can call any function in an arrow function? Commented Jun 24, 2020 at 14:07
  • 1
    settVolume is declared inside on that event, it is not going to be reachable outside that block. It does not make sense to be declared inside. What is the purpose for it to be inside? Commented Jun 24, 2020 at 14:08
  • you would have to make all of this global, or edit the code in the inspector, all console code runs like ti was tacked onto the bottom of your code Commented Jun 24, 2020 at 14:08
  • 1
    Does this answer your question? How do JavaScript closures work? Commented Jun 24, 2020 at 14:08
  • I dont know, Im new in stuff like Arrow functions Commented Jun 24, 2020 at 14:10

1 Answer 1

1

So how can I call the setVolume function from the console?

Fundamentally, you can't, not least because there isn't just one of them. There's a new setVolume function created every time the state change callback is run. Each of those functions is local to that state change callback and not accessible outside it. It would possible to have a global variable that you updated with the latest copy of the function each time one was created, but that's not likely to be a good idea. The function is presumably local for a reason.

Sign up to request clarification or add additional context in comments.

1 Comment

Okay, that makes sense. Thank you, I'll try

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.