反过来做:
<script>
numberofArticles = 5;
db = openDatabase("websql", "0.1", "web-sql testing", 10000);
db.transaction(function(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, articleID int)');
});
db.transaction(function (tx) {
for (var i=0; i<=numberofArticles-1; i++){
tx.executeSql('INSERT INTO LOGS (articleID) VALUES (?)', [i]);
};
});
</script>
另一种选择是,在这种情况下,外部循环的正确方法是不必要的
for (var i=0; i<=numberofArticles-1; i++){
(function(i) {
db.transaction(function (tx) {
tx.executeSql('INSERT INTO LOGS (articleID) VALUES (?)', [i]);
});
})(i);
};