如何使javascript函数等待继续之前完成websql事务

时间:2014-01-14 15:21:17

标签: javascript jquery ajax

我正在尝试做一个非常简单的任务,找出哪个websql表有最新日期的行。我试图使用延迟的jquery帮助我这样做,但由于某种原因它只是不工作。

有两个平台表,FORUM和BLOG。以下代码获取每个平台的最新发布日期,然后应该查看哪个更新,并将全局变量window.lastplatform设置为“blog”或“forum”。

我一直在尝试各种各样的事情和方法,但似乎没有任何效果。

function lastPlatform()
{
    $.when( setLastPostDates ).then( setLastPlatform(), alert('failed') );
}

function setLastPlatform()
{
    var blogYear = window.lastblogpostdate.substring(0, 4);
    var blogMonth = window.lastblogpostdate.substring(5, 7);
    var blogDay = window.lastblogpostdate.substring(8, 10);
    var blogHours = window.lastblogpostdate.substring(11, 13);
    var blogMinutes = window.lastblogpostdate.substring(14, 16);
    var blogSeconds = window.lastblogpostdate.substring(17);

    var forumYear = window.lastforumpostdate.substring(0, 4);
    var forumMonth = window.lastforumpostdate.substring(5, 7);
    var forumDay = window.lastforumpostdate.substring(8, 10);
    var forumHours = window.lastforumpostdate.substring(11, 13);
    var forumMinutes = window.lastforumpostdate.substring(14, 16);
    var forumSeconds = window.lastforumpostdate.substring(17);

    lastblogpostdate = new Date(blogYear, blogMonth, blogDay, blogHours, blogMinutes, blogSeconds);
    lastforumpostdate = new Date(forumYear, forumMonth, forumDay, forumHours, forumMinutes, forumSeconds);
    if (lastblogpostdate > lastforumpostdate) {
        window.lastplatform = 'blog';
    } else {
        window.lastplatform = 'forum';
    }
}

function setLastPostDates() 
{
    if(!db){
        db=window.openDatabase("postdb","1.0","Posts",200000);
    }

    db.transaction(queryLastBlogPostDate,errorCB);
    db.transaction(queryLastForumPostDate,errorCB);
}

function queryLastBlogPostDate(tx)
{
    tx.executeSql('SELECT post_date FROM BLOG ORDER BY post_date DESC LIMIT 1',[], function(tx, results){
        window.lastblogpostdate = results.rows.item(0).post_date;
        alert(window.lastblogpostdate);
    },errorCB);
}

function queryLastForumPostDate(tx)
{
    tx.executeSql('SELECT post_date FROM FORUM ORDER BY post_date DESC LIMIT 1',[],     function(tx, results){
        window.lastforumpostdate = results.rows.item(0).post_date;
        alert(window.lastforumpostdate);
    },errorCB);
}

我会很感激任何信息,提示或帮助。

0 个答案:

没有答案