1

I have trouble get this working:

this.getTime = function(val) {
    function getDuration(){
            self.player.api("getDuration", function(data){
                var d = data, c=val * d;
                return [c,d];
            });
        }
    return getDuration();
}

Obviously there is a delay for get duration value. Do I have to use some kind of timer?

Toniq
  • 4,492
  • 12
  • 50
  • 109
  • `self.player.api()` is asynchronous – adeneo Dec 20 '15 at 18:14
  • 1
    "[How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call)" – Jonathan Lonowski Dec 20 '15 at 18:17
  • Side note: `return` statements apply to the nearest `function` above them. In this case, that's the anonymous `function(data) {...}`, not `function getDuration() {...}`. And, the call to the anonymous callback is within `self.player.api()`, so the array is given to it instead. (Though, moving the `return` statement to fix this won't resolve the conflict with asynchronous execution.) – Jonathan Lonowski Dec 20 '15 at 18:20

1 Answers1

1

You cannot do this on an Asynchronous function. The self.player.api("getDuration") function is asynchronous and you cannot return a value from it. So create a new function and then call it instead.

this.getTime = function(val) {
    function getDuration(){
            self.player.api("getDuration", function(data){
                var d = data, c=val * d;
                postGetDuration([c,d]);
            });
        }
}
function postGetDuration(getDurationValue) {
  // do whatever
}
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252