Return 带有回调的变量
Return variable with a 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': 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
。但它是 returning undefined
.
所以我的问题是,我如何 return 在回调中传递相同的值,因为我传入 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
}
});
比方说,我有一个函数:
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
。但它是 returning undefined
.
所以我的问题是,我如何 return 在回调中传递相同的值,因为我传入 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
}
});