3

I can`t return result of this function.

function get_duration() {
    var a = '';
    $.ajax({
        url: "http://gdata.youtube.com/feeds/api/videos?q=3KMz3JqRByY&max-results=50& format=5,1,6",
        dataType: "jsonp",
        success: function (data) {
            re2 = /seconds='(\d+)'/ig;
            while (re.exec(data) != null) {
                a = re2.exec(data);
            }
        }
    });
    return a;
}
Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
user3313127
  • 93
  • 2
  • 10

1 Answers1

1

You have to use return inside the success callback since, A in Ajax is asynchronous.

Like this:

function get_duration() {
    var a = '';
    $.ajax({
        url: "http://gdata.youtube.com/feeds/api/videos?q=3KMz3JqRByY&max-results=50& format=5,1,6",
        dataType: "jsonp",
        success: function (data) {
            re2 = /seconds='(\d+)'/ig;
            while (re.exec(data) != null) {
                a = re2.exec(data);
            }
            return a;
        }
    });
}

But, this function isn't guaranteed to return. You'll have to use a callback function kind of thing.

Amit Joki
  • 58,320
  • 7
  • 77
  • 95