正在尝试在设备上执行代码

Trying to execute code on device ready

本文关键字:执行 代码      更新时间:2023-09-26

我正试图使用监听器在deviceready上执行一些Javascript代码。

它似乎没有调用代码——我的控制台中什么都没有得到,也没有设置任何变量。

这是我使用的代码示例:

    <script>
                // Cordova is ready
function onDeviceReady() {
  var db = window.sqlitePlugin.openDatabase({name: "my.db"});
  db.transaction(function(tx) {
    tx.executeSql('DROP TABLE IF EXISTS test_table');
    tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)');
    tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function(tx, res) {
      console.log("insertId: " + res.insertId + " -- probably 1");
      console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");
      tx.executeSql("select count(id) as cnt from test_table;", [], function(tx, res) {
        console.log("res.rows.length: " + res.rows.length + " -- should be 1");
        console.log("res.rows.item(0).cnt: " + res.rows.item(0).cnt + " -- should be 1");
      });
    }, function(e) {
      console.log("ERROR: " + e.message);
    });
  });
}
// Wait for Cordova to load
document.addEventListener("deviceready", onDeviceReady, false);
</script>

现在,我主要不是一个Javascript开发人员(所以这个问题可能很简单),但代码没有执行有什么原因吗?我必须在匿名函数中运行它吗?

如何执行onDeviceReady()函数?

deviceready是一个特殊的科多瓦事件;除非您在Cordova环境中启动代码,否则事件永远不会被触发(您的代码也永远不会被执行)。

可以通过将浏览器添加为平台,在浏览器中运行Cordova。有关更多信息,请参阅http://www.raymondcamden.com/2014/09/24/Browser-as-a-platform-for-your-PhoneGapCordova-apps.

您也可以使用类似Ripple的东西来模拟Chrome中的Cordova环境。

由于Cordova-SQLitePlugin API是异步的,所以应该在数据库可用/创建后执行事务。尝试以如下类似的方式实现它:

    var openDb = function (name, ok, error) {
      try {
        // SQLitePlugin always uses callbacks to specify the status of 'open' operation
        var dbRef = window.sqlitePlugin.openDatabase({name: name},
          function (db) {
            log("DataBase " + name + " opened!");
            ok(db);
          }, function (e) {
            try {
              dbRef.close();
            } catch (e) {
              log("Could not close database", e);
            }
            error(e);
          });
      } catch (e) {
        log("Could not open " + name + " DataBase");
        error(e);
      }
    };
    openDb("my.db", function(db){
       db.transaction(function(tx) {
        tx.executeSql('DROP TABLE IF EXISTS test_table');
        tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)');
        tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function(tx, res) {
          console.log("insertId: " + res.insertId + " -- probably 1");
          console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");
          tx.executeSql("select count(id) as cnt from test_table;", [], function(tx, res) {
            console.log("res.rows.length: " + res.rows.length + " -- should be 1");
            console.log("res.rows.item(0).cnt: " + res.rows.item(0).cnt + " -- should be 1");
          });
        }, function(e) {
          console.log("ERROR: " + e.message);
        });
      });
    });