我正在构建一个显示播放按钮或停止按钮的表格。(和其他一些东西)
for(var i = 0; i < result.length; i++){
var current = result[i].split("$");
if (CheckRunning(current[0])){
t = t + "<tr><td><img alt='stop' id='img"+i+"' src='stop.png' onclick='ChangeButton(\"img"+i+"\");'/>";
} else {
t = t + "<tr><td><img alt='play' id='img"+i+"' src='play.png' onclick='ChangeButton(\"img"+i+"\");'/>";
}}
这里的问题是 CheckRunning 方法。它打开一个数据库,这是一个异步方法。我不能简单地返回真/假。那么解决方案是什么?无论如何,这是它的代码:
var tabel;
var running = false;
function CheckRunning(tabel){
this.tabel = "tabel"+tabel+"";
var db = window.openDatabase(this.tabel, "1.0", this.tabel, 1000000);
db.transaction(checkrunningDB, checkerrorCB);
console.log(this.running);
return this.running;
}
function checkrunningDB(tx) {
tx.executeSql('SELECT max(id), sluttime FROM '+this.tabel, [], checkrunningSuccess, checkerrorCB);
}
function checkrunningSuccess(tx, results) {
if (results.rows.item(0).sluttime != null){
this.running = false;
} else{
this.running = true;
}
}
function checkerrorCB(err) {
this.running = false;
console.log(err);
}