gpt4 book ai didi

javascript - jquery如何将参数传递给回调函数到管道中

转载 作者:行者123 更新时间:2023-11-27 23:18:34 26 4
gpt4 key购买 nike

我有下面的简单示例:

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'));

结果:参数 = 确定21我失去了功能之间的同步。如何通过同步将参数传递给 secondaryFunction 到管道中?

最佳答案

好吧,你需要做一些小改变:

Your code will execute secondFunction immediately and pass the return value from executing it as the argument to firstFunction 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com