我有以下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(); }