原型PeriodicExecutor不更新源

时间:2009-05-22 21:37:13

标签: javascript prototypejs

我有一个脚本,当数据库中有新条目时更新表。它从表格的最后一行拉出一段时间,然后抓取从那时起输入的所有条目。

第一次运行正常,它抓取新条目并更新表。但是,当它第二次运行时,它将使用该初始时间戳,而不是检索刚刚从数据库中提取的当前最后一个条目。

所以似乎PeriodicalExecuter函数在启动时加载页面的html并且无法解释任何异步更新,或者它只是保存了初始时间。反正有没有绕过这个?

以下是一些代码:

new PeriodicalExecuter(function() {
      new Ajax.Request(
        '#{url}&since_ts=' + $$('#posts tr:last-child td.description')[0].innerHTML.match(/Posted at: (\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2})/)[1], 
        {asynchronous:true, 
          evalScripts:true, 
          parameters:'authenticity_token=' + encodeURIComponent('#{form_authenticity_token}')
        }
      )
    }, 60)

我对javascript很新,我非常感谢任何帮助。

谢谢!

2 个答案:

答案 0 :(得分:0)

之前我没有使用Prototype,所以这是一个疯狂的猜测,但它可能只是缓存调用,因为它默认为GET。尝试以下参数是您在url之后的调用

{
    asynchronous:true, 
    method: 'post',
    evalScripts:true, 
    parameters:'authenticity_token=' + encodeURIComponent('#{form_authenticity_token}')
}

答案 1 :(得分:0)

我发现问题不在于PeriodicExecutor函数,而在于我的选择器语句。所以我所要做的就是找到另一种方法来访问表中的最后一个tr元素。

$$('#posts tr td.description').last().innerHTML.match(/Posted at: (\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/)[1]

如此简单,但它始终是你看到的最后一件事。