PeriodicExecutor prototype does not update source

I have a script that updates a table when there are new records in the database. It pulls the time from the last row in the table and then grabs all the records that have been entered since then.

It works fine the first time, it grabs new records and updates the table. However, when it is executed a second time, it will use that starting timestamp and will not fetch the current last record that was simply fetched from the database.

So the PeriodicalExecuter function loads the html page on startup and doesn't account for any asynchronous updates, or just keeps that start time. Is there a way to get around this?

Here is the code:

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)

      

I am new to javascript and I would really appreciate any help.

Thanks!

0


a source to share


2 answers


I realized that the problem was not with the PeriodicExecutor function, but rather with the selector operator. So I needed to find another way to access the last tr element in the table.

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

      



So simple and yet it's always the last thing you look at.

0


a source


I haven't used Prototype before, so it's a wild guess, but it might just cache the call as it does a GET by default. Try the following parameters: your call after url



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

      

0


a source







All Articles