gpt4 book ai didi

javascript - 相同的嵌套查询

转载 作者:行者123 更新时间:2023-11-30 05:54:26 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
javascript: Using the current for-loop counter-value inside a function() { }?

如果我运行下面的代码,第二个控制台日志输出只是重复第一个控制台日志输出的最后一行。

据我所知,这是因为所有嵌套的查询 2 都排队等候在其他一切都完成后运行。此外,由于我们在函数中有一个函数,这会创建一个“闭包”,这意味着嵌套查询 2 只有一组变量,并且使用的是这些变量的最终状态,因此只有最后一个查询 1 的结果是反复用于查询 2。至少我认为这是正在发生的事情。问题是,我怎样才能改变它,使整个事情按顺序进行?

谢谢!

db.transaction(function(tx){

// Query 1
tx.executeSql("SELECT * FROM Products GROUP BY ssrt55", [], function(tx, listResults){
for (var i = 0; i < listResults.rows.length; i++) {
var lineData = listResults.rows.item(i);
var productDescriptionSQL = "select * from ProductDescriptions where bsrt56 = " + lineData['SSRT55'];
console.log(productDescriptionSQL);

// Query 2
tx.executeSql(productDescriptionSQL, [], function(tx, descriptionResults){
console.log(productDescriptionSQL);
}, onError);

}
}, onError);

});
First console log output
select * from ProductDescriptions where bsrt56 = 1.00
select * from ProductDescriptions where bsrt56 = 2.00
select * from ProductDescriptions where bsrt56 = 2.50
select * from ProductDescriptions where bsrt56 = 3.00
select * from ProductDescriptions where bsrt56 = 4.00

Second console log output
select * from ProductDescriptions where bsrt56 = 4.00
select * from ProductDescriptions where bsrt56 = 4.00
select * from ProductDescriptions where bsrt56 = 4.00
select * from ProductDescriptions where bsrt56 = 4.00
select * from ProductDescriptions where bsrt56 = 4.00

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