2013-05-21 50 views
0

我有一個索引頁面,我想用它來設置本地數據庫,然後再轉到其他頁面。但是,每當我激活window.location代碼時,其他函數都不會運行,但是當我將其註釋掉時,其他函數運行良好。任何想法會導致什麼,以及如何讓功能和window.locations工作?代碼如下:推遲window.location以允許AJAX的時間

<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); 
     } 
    } 
}); 
} 
+0

你的 「加載」 功能可能啓動異步活動。您需要將頁面重新加載到HTTP請求已完成的位置。您的具體做法取決於您使用的代碼。 – Pointy

+0

在設置window.location之前立即發出的ajax請求在頁面開始加載Application.html之前不會返回,但它們應該仍然會觸發 –

回答

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'; 
    }); 
+0

好的,我明白了。推遲window.location請求的最佳方式是什麼?我添加了兩個被使用的函數。 – EzAnalyst

+0

這些功能是否在同一個頁面/腳本中?是executeSql異步? –

+0

感謝您不偷我的代碼。 – Zim84

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(); } 
     } 
    }); 
}