我有下面的简单示例:
function firstFunction(){
var d = jQuery.Deferred();
// some very time consuming asynchronous code...
setTimeout(function() {
console.log('1');
d.resolve();
}, 1000);
return d.promise();
}
function secondFunction(param){
console.log('parm = '+param);
var d = $.Deferred();
setTimeout(function() {
console.log('2');
d.resolve();
}, 10);
return d.promise();
}
firstFunction().pipe(secondFunction('OK'));
结果: 参数 = 确定 2 1 我失去了功能之间的同步。 如何通过同步将参数传递给 secondaryFunction 到管道中?
最佳答案
好吧,你需要做一些小改变:
Your code will execute
secondFunction
immediately and pass the return value from executing it as the argument tofirstFunction
which is unlikely what you want.
阅读完整答案:Javascript callback function with parameters
console.log = function(message) {
$('body').append('<div>' + message + '</div>');
}
function firstFunction(){
var d = jQuery.Deferred();
// some very time consuming asynchronous code...
setTimeout(function() {
console.log('1');
d.resolve();
}, 1000);
return d.promise();
}
function secondFunction(param){
console.log('parm = '+param);
var d = $.Deferred();
setTimeout(function() {
console.log('2');
d.resolve();
}, 10);
return d.promise();
}
firstFunction().then(function() {
secondFunction('OK')
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
关于javascript - jquery如何将参数传递给回调函数到管道中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35604050/