繁体   English   中英

执行另一个函数后运行javascript函数

[英]Run javascript function after excecution of another function

我具有某些子页面中的功能,

function fuct(){

....
somecode inside
}

我想做的是在成功执行功能在HTML页面中的funct之后,执行另一个称为function anotherfunction的功能。 而且我不想在anotherfunction体中调用anotherfunction函数。

我可以做点什么吗,

函数的on.succss调用另一个函数

缺少的关键字是callback

你可以有这样的事情:

function funct(callback) {
   // do something
   callback(a, b, c, ..., n);
}

function anotherFunction(a, b, c, ..., n) {
   // do something
}

funct(anotherFunction);

JSFIDDLE


另一种方法是使用诺言:

function func() {
    var deferred = new $.Deferred();

    // do something
    deferred.resolve(position);


    // return promise so that outside code cannot reject/resolve the deferred
    return deferred.promise();
};

funct().then(anotherFunction);

做类似的事情

function function1()
{
    //if succeeds
    return true;
}

那你需要的地方

if (function1()) {
    //call another function
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM