Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 异步库ForLoop_Javascript_Loops_Asynchronous - Fatal编程技术网

Javascript 异步库ForLoop

Javascript 异步库ForLoop,javascript,loops,asynchronous,Javascript,Loops,Asynchronous,我尝试运行这段代码,并使用异步库让say.speak命令一次运行一个。但实际上,speak命令都是同时发生的。但是console.log的发生顺序是正确的!有人知道解决办法吗 控制台解决方案(时间和事件名称): 下午12:00:00 马赫和马特 下午4:00:00 设置帮助!微软社交3.0芝加哥 下午4:30:00 AV检查-Microsoft社交3.0活动芝加哥 代码 var api = require('./api.js'); var say = require('say'); var as

我尝试运行这段代码,并使用异步库让say.speak命令一次运行一个。但实际上,speak命令都是同时发生的。但是console.log的发生顺序是正确的!有人知道解决办法吗

控制台解决方案(时间和事件名称):

下午12:00:00

马赫和马特

下午4:00:00

设置帮助!微软社交3.0芝加哥

下午4:30:00

AV检查-Microsoft社交3.0活动芝加哥

代码

var api = require('./api.js');
var say = require('say');
var async = require('async');
var S = require('string');
var i = 0;

api.getEvents(function (err, result) {
    if (err)
        console.log(err);
    else {
        //Intro();

        async.times(result.value.length, function (callback) {
            async.series([
                function (callback) { Time(result.value[i].start.dateTime, callback);},
                function (callback) { someAsyncTask(result.value[i].subject, callback); ++i;  },


            ])
        });

    }

});
// do two async series.. one for setup and one for read
// series 1: time convert, and send result.value[i] to series 2
// series 2: for loop to read out 
// optional callback

function Intro() {
    setTimeout(function () {
        say.speak('todays schedule is');
    }, 1000);
}

function someAsyncTask(event, callback) {
    setTimeout(function () {
        console.log(event);
        say.speak(event);
        // * returning err !== null will abort the execution
        // of all tasks and call finalCallback(err)
        // * result will be appended to the list of results returned to finalCallback()
        callback(/*err=*/null, /*result=*/event);
    }, 2000);
}


function Time(data, callback) {
    var cst = new Date(Date.parse(data));
    var time = cst.toLocaleTimeString();
    setTimeout(function () {
        console.log(time);
        say.speak(time);
    }, 2000)

    callback();
}

在这段时间里,他并不是很快,也不引人注目。尝试在每次迭代后增加超时值,您将看到差异。希望这能有所帮助。在这段时间里,我们的速度不是很快,也不是很明显。尝试在每次迭代后增加超时值,您将看到差异。希望这有帮助。