2013-05-21 55 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(); } 
     } 
    }); 
}