1

Lets say, I have a function:

function my_function(some_variable, count, callback) {
    // some code here, doing jQuery animation, transition, etc., f.e:

    var temp = $('#some_element').clone().appendTo('body');

    temp.animate({'top': newOffset.top, 'left': newOffset.left }, 'slow', function() {
        if (callback !== undefined) {
            callback();

            return count;
        }
        else {
            return count;
        }
    });
}

So when I call it f.e like this:

my_function('test', 75, function(result) {
    console.log(result);
});

I need to have in the console 75. But it is returning undefined.

So my question is, how I can return in callback the same value, as I passed in the count?

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Legionar
  • 7,472
  • 2
  • 41
  • 70

1 Answers1

0

You need to pass variable count as parameter to callback function

temp.animate({'top': newOffset.top, 'left': newOffset.left }, 'slow', function() {
    if (callback !== undefined) {
        callback(count); //Pass whatever value you want to callback method
    }        
});
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • It's duplicate of http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call and should have been closed – Tushar Oct 13 '15 at 09:17
  • 1
    @Tushar, I disagree, as OP is already using `callback()` function, his problem is he is not able to pass `count`. Not how to use `callback` method. – Satpal Oct 13 '15 at 09:26
  • @Satpal But the answers on the question actually answers this question indirectly – Tushar Oct 13 '15 at 09:28