等待循环完成JavaScript

时间:2018-03-31 09:52:16

标签: javascript asynchronous callback

这是我的代码,我只需要在循环结束时调用回调。

function insertIntoCacheRange(userID, from, to, callback){
    var dataRanges = new Array();
    for(let pointer=from,  index = parseInt(from) + 1800 ; index < to; pointer+=1800, index +=1800){
        var count = 0;
        var sqlSelect = "SELECT count(*) as count FROM sensordata WHERE sensorid = ? AND time >= ? AND time < ?";
            con.query(sqlSelect, [userID, pointer, index], function(err, result){
                count = JSON.parse(JSON.stringify(result))[0].count;
                //console.log("pointer + " + pointer + " limit " + index + " = " +count);
                dataRanges.push(count);
                var sqlInsert = "INSERT INTO cache1800 (sensorid, time, count) VALUES(?,?,?)";
                con.query(sqlInsert, [userID, pointer, count],  function(err, result){
                    if(err) throw  err;

                });
            });      
    }
    callback(dataRanges);
}

解决!!最后我想出来就是这样:

function insertIntoCacheRange(userID, from, to, callback){
    var dataRanges = new Array();
    for(let pointer=from,  index = parseInt(from) + 1800 ; index < to; pointer+=1800, index +=1800){
        var count = 0;
        var sqlSelect = "SELECT count(*) as count FROM sensordata WHERE sensorid = ? AND time >= ? AND time < ?";
            con.query(sqlSelect, [userID, pointer, index], function(err, result){
                count = JSON.parse(JSON.stringify(result))[0].count;
                //console.log("pointer + " + pointer + " limit " + index + " = " +count);
                dataRanges.push(count);
                var sqlInsert = "INSERT INTO cache1800 (sensorid, time, count) VALUES(?,?,?)";
                con.query(sqlInsert, [userID, pointer, count],  function(err, result){
                    if(err) throw  err;
                });
                //We reached the end of the loop (JS, you and your sync behaviour are crazy)
                if(dataRanges.length == 31){
                    callback(dataRanges);
                }
            });      
    }
}

我需要检查阵列是否已经是31尺寸。然后就结束了。非常感谢你的想法:)

1 个答案:

答案 0 :(得分:1)

您可以检查dataRanges长度是否等于或大于可能的迭代次数

function insertIntoCacheRange(userID, from, to, callback, errorCallback)
{
    var dataRanges = new Array();
    var numberOfIterations = (from-to)/1800; //find out number of possible iterations
    for(let pointer=from,  index = parseInt(from) + 1800 ; index < to; pointer+=1800, index +=1800){
        var count = 0;
        var sqlSelect = "SELECT count(*) as count FROM sensordata WHERE sensorid = ? AND time >= ? AND time < ?";
            con.query(sqlSelect, [userID, pointer, index], function(err, result){
                count = JSON.parse(JSON.stringify(result))[0].count;
                //console.log("pointer + " + pointer + " limit " + index + " = " +count);                 
                dataRanges.push(count);
                var sqlInsert = "INSERT INTO cache1800 (sensorid, time, count) VALUES(?,?,?)";
                con.query(sqlInsert, [userID, pointer, count],  function(err, result){
                    if(err) { errorCallback() }; //invoke error callback if there is an error                
                });
                //if dataRanges length has reached that value then callback
                if ( numberOfIterations <= dataRanges.length )
                {
                   callback(dataRanges);
                }
            });      
    }
}