Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 返回带有回调的变量_Javascript_Jquery_Callback - Fatal编程技术网

Javascript 返回带有回调的变量

Javascript 返回带有回调的变量,javascript,jquery,callback,Javascript,Jquery,Callback,比方说,我有一个函数: 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': new

比方说,我有一个函数:

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;
        }
    });
}
所以当我这样称呼它f.e时:

my_function('test', 75, function(result) {
    console.log(result);
});
我需要在控制台中安装
75
。但它返回的是
未定义的


所以我的问题是,如何在回调中返回相同的值,就像我在
count
中传递一样?

您需要将变量
count
作为参数传递给
callback
函数

temp.animate({'top': newOffset.top, 'left': newOffset.left }, 'slow', function() {
    if (callback !== undefined) {
        callback(count); //Pass whatever value you want to callback method
    }        
});

您需要将变量
count
作为参数传递给
callback
函数

temp.animate({'top': newOffset.top, 'left': newOffset.left }, 'slow', function() {
    if (callback !== undefined) {
        callback(count); //Pass whatever value you want to callback method
    }        
});

它是的复制品,应该是closed@Tushar,我不同意,因为OP已经在使用
callback()
函数,他的问题是他无法传递
count
。不是如何使用
callback
方法。@Satpal但是这个问题的答案实际上间接地回答了这个问题。它是重复的,应该是重复的closed@Tushar,我不同意,因为OP已经在使用
callback()
函数,他的问题是他无法传递
count
。不是如何使用
callback
方法。@Satpal但问题的答案实际上间接地回答了这个问题