我有以下 JavaScript 代码:
$('a.button').click(function(){ if (condition == 'true'){ function1(someVariable); function2(someOtherVariable); } else { doThis(someVariable); } });
如何确保function2仅在function1完成后调用?
function2
function1
指定一个匿名回调,并让 function1 接受它:
$('a.button').click(function(){ if (condition == 'true'){ function1(someVariable, function() { function2(someOtherVariable); }); } else { doThis(someVariable); } }); function function1(param, callback) { ...do stuff callback(); }