javascript - 当一个或多个函数完成时调用一个函数

标签 javascript

我需要一些帮助...我在 javascript 中有 5 个函数,但我无法在前面的函数之后调用第五个函数。 我有一些这样的:

function one(){

}
function two(){

}
function three(){

}
function four(){

}
function five(){

}

这段代码,在其他函数之前执行函数“five()”。我希望 five() 仅在所有先前的功能完成后才执行。 我怎样才能做到这一点?

编辑: 这是我的电话

function callFunction(){
    one();
    two();
    three();
    four();
    five();
}

最佳答案

最好的解决方案是使用 promise 。异步进程的 promise 是非常容易的。你需要 Promise.all(iterable)。

The Promise.all(iterable) method returns a promise that resolves when all of the promises in the iterable argument have resolved, or rejects with the reason of the first passed promise that rejects.

var p1 = Promise.resolve(3);
var p2 = 1337;
var p3 = new Promise(function(resolve, reject) {
  setTimeout(resolve, 100, "foo");
}); 

Promise.all([p1, p2, p3]).then(function(values) { 
  console.log(values); // [3, 1337, "foo"] 
});

检查这个:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

如果你想要 IE 兼容性,你可以使用 q。

https://github.com/kriskowal/q

q 也有 .all 。

我为你写了这个例子:

/**
 * Example of Q.all usage
 * @author Georgi Naumov
 * gonaumov@gmail.com for contacts and
 * suggestions.
 */
var firstStep = (function () {
    var dfd = Q.defer();
    setTimeout(function () {
        console.log("first step  finished");
        dfd.resolve();
    }, 1000);
    return dfd.promise;
}());

var secondStep = (function () {
    var dfd = Q.defer();
    setTimeout(function () {
        console.log("second step  finished");
        dfd.resolve();
    }, 3000);
    return dfd.promise;
}());

Q.all([firstStep, secondStep]).then(function () {
    console.log('All done!');
});

console.log('全部完成!');将在所有 asinc 任务完成后执行。

这是 jsfiddle:

http://jsfiddle.net/drv4538t/3/

此解决方案与时间间隔无关。检查这个例子:

/**
 * Example of Q.all usage
 * @author Georgi Naumov
 * gonaumov@gmail.com for contacts and
 * suggestions.
 */

/**
 * This function is taken from:
 * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
 */
function getRandomIntInclusive(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

var firstStep = (function () {
    var dfd = Q.defer();
    setTimeout(function () {
        console.log("first step  finished");
        dfd.resolve();
    }, getRandomIntInclusive(1000, 10000));
    return dfd.promise;
}());

var secondStep = (function () {
    var dfd = Q.defer();
    setTimeout(function () {
        console.log("second step  finished");
        dfd.resolve();
    }, getRandomIntInclusive(1000, 10000));
    return dfd.promise;
}());

Q.all([firstStep, secondStep]).then(function () {
    console.log('All done!');
});  

这是 jsfiddle:

http://jsfiddle.net/drv4538t/4/

关于javascript - 当一个或多个函数完成时调用一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35334544/

相关文章:

javascript - 带有变换旋转的顶部和左侧位置

javascript - 使用 ng-click 过滤博客文章

javascript - 溢出和滚动?

javascript - 在 VueJS 2 和 JSX 中将展开运算符与事件监听器结合使用

javascript - 对数组中的数据进行分组

javascript - 如何交换 HTML 代码中 div 的顺序?

javascript - 原型(prototype)/脚本的悬停意图

javascript - 在 HTML 文档中使用 XML 文件中的数据

javascript - 设置自定义 HTML5 有效性消息属性会忽略模式正则表达式

javascript - 是否可以跟踪离开或转至其他网站的访问者?