gpt4 book ai didi

javascript - 等待一个循环结束 JavaScript

转载 作者:行者123 更新时间:2023-11-30 20:38:13 25 4
gpt4 key购买 nike

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

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 大小。然后它就完成了。非常感谢你的想法:)

最佳答案

您可以检查 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);
}
});
}
}

关于javascript - 等待一个循环结束 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49586445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com