0

我有一个索引页面,我想用它来设置本地数据库,然后再转到另一个页面。但是,每当我激活 window.location 代码时,其他函数都不会运行,但是当我将其注释掉时,其他函数运行正常。有什么想法会导致这种情况以及如何让函数和 window.locations 工作?代码如下:

<script>
        var db = window.openDatabase("DB1", "", "DB", 1024 * 1000)
            CreateDB(); //Creates local database tables
            loadRouteList(); //Queries web server database using AJAX and inserts Routes
            window.location = 'Application.html';
</script>

使用的功能:

function CreateDB() {
    db.transaction(function (tx) {
        tx.executeSql('CREATE TABLE IF NOT EXISTS Routes(id INTEGER PRIMARY KEY, routeID TEXT, customerID TEXT, stopSeq TEXT, driverID TEXT)', []);
    });
};
function loadRouteList() {
var dataObject = {
    postDesignator: 'routes',
};
$.ajax({
    url: 'http://url.php',
    data: dataObject,
    dataType: 'json',
    type: 'post',
    success: function (Result) {
        for (var i = 0, len = Result.records.length; i < len; ++i) {
            var route = Result.records[i].record;
            insertRoute(route.routeID, null, null, null);
        }
    }
});
}
4

2 回答 2

2

使用回调!我修改了你的代码:

<script>
    var db = window.openDatabase("DB1", "", "DB", 1024 * 1000);
    CreateDB(); //Creates local database tables
    loadRouteList(function() { window.location = 'Application.html'} );
</script>

使用的功能:

function CreateDB() {
    db.transaction(function (tx) {
        tx.executeSql('CREATE TABLE IF NOT EXISTS Routes(id INTEGER PRIMARY KEY, routeID TEXT, customerID TEXT, stopSeq TEXT, driverID TEXT)', []);
    });
};
function loadRouteList(callback) {
    var dataObject = {
        postDesignator: 'routes',
    };
    $.ajax({
        url: 'http://url.php',
        data: dataObject,
        dataType: 'json',
        type: 'post',
        success: function (Result) {
            for (var i = 0, len = Result.records.length; i < len; ++i) {
                var route = Result.records[i].record;
                insertRoute(route.routeID, null, null, null);
            }
            // this is the so called callback, that gets executed AFTER the ajax has finished
            if(callback) { callback(); }
        }
    });
}
于 2013-05-21T20:58:25.607 回答
1

根据定义,AJAX 是Asynchronous,因此如果您运行这些函数并且不等待它们完成,您的代码将继续运行而无需等待它们。所以你到达了你的位置由于你的线路而改变的地方。您必须等到所有请求都完成后再继续,为此您必须更改函数内的代码。如果您发布它们,我们可以帮助您。

编辑

在我看来,最好的方法是将回调传递给您的函数:

function CreateDB() {
    db.transaction(function (tx) {
        tx.executeSql('CREATE TABLE IF NOT EXISTS Routes(id INTEGER PRIMARY KEY, routeID TEXT, customerID TEXT, stopSeq TEXT, driverID TEXT)', []);
    });
    //if even this piece of code is async you should read docs and check how to call a function after the query executed
};
function loadRouteList(callback) {
    var dataObject = {
        postDesignator: 'routes',
    };
    $.ajax({
        url: 'http://url.php',
        data: dataObject,
        dataType: 'json',
        type: 'post',
        success: function (Result) {
            for (var i = 0, len = Result.records.length; i < len; ++i) {
                var route = Result.records[i].record;
                insertRoute(route.routeID, null, null, null);
            }
            if(callback) {
                callback();
            }
        }
    });
}

然后以这种方式使用它:

var db = window.openDatabase("DB1", "", "DB", 1024 * 1000)
    CreateDB(); //Creates local database tables
    loadRouteList(function() {
        window.location = 'Application.html';
    });
于 2013-05-21T20:35:12.403 回答