按钮onClick事件和Href-Location

我在本讨论中使用了“Chrules”给出的解决方案: 没有href的按钮,onclick等

像这样 :

 ...... $("#clickable").click(function(){ // Send user to this link insertRecord(); //SQLite Code location.href = "game.html"; // location.href = "http://www.takemehere.com"; }); 

所以,当我使用location.href时就像他推了它一样(=“ http://www.takemehere.com ”;)insertRecord()方法成功完成(该对象被添加到数据库中)

但是当我使用location.href =“game.html”时; game.html页面打开但是insertRecord()方法没有完成我试图把game.html的完整路径,但同样的问题仍然存在你有什么想法吗? 提前谢谢你

我认为insertMethod()调用一些异步函数来进行数据库更新。 所以你应该在做任何其他事情之前使用回调。

像这样的东西(我不知道insertRecord方法里面是什么);

 function insertRecord(callback) { // do all sorts of database job // when it's finished // fire your callback callback(); } insertRecord(function() { location.href = "page.html"; }); 

编辑:在你的评论之后,我看到你已经定义了一个在sql之后运行的函数,即loadAndReset() 。 所以只需将location.href = "page.html"放在该函数中即可。