您可以使用jQuery的
$.Deferred
var FunctionOne = function () { // create a deferred object var r = $.Deferred(); // do whatever you want (e.g. ajax/animations other asyc tasks) setTimeout(function () { // and call `resolve` on the deferred object, once you're done r.resolve(); }, 2500); // return the deferred object return r;};// define FunctionTwo as neededvar FunctionTwo = function () { console.log('FunctionTwo');};// call FunctionOne and use the `done` method// with `FunctionTwo` as it's parameterFunctionOne().done(FunctionTwo);您还可以将多个延期打包在一起:
var FunctionOne = function () { var a = $.Deferred(), b = $.Deferred(); // some fake asyc task setTimeout(function () { console.log('a done'); a.resolve(); }, Math.random() * 4000); // some other fake asyc task setTimeout(function () { console.log('b done'); b.resolve(); }, Math.random() * 4000); return $.Deferred(function (def) { $.when(a, b).done(function () { def.resolve(); }); });};


