Tutorial :Prototype PeriodicExecutor doesn't update source



Question:

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

It runs fine the first time, it grabs the new entries and updates the table. When it runs the second time, however, it will use that initial timestamp and not retrieve the current last entry that was just pulled from the database.

So it seems like the PeriodicalExecuter function loads the page's html when it starts and fails to account for any asynchronous updates or it merely saves that initial time. Is there anyway to get around this?

Here's some 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 fairly new to javascript and I would greatly appreciate any help.

Thanks!


Solution:1

I've not used Prototype before, so this is a wild guess, but it could just be caching the call as it's defaulting to GET. Try the following parameters is your call after the url

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


Solution:2

I figured out that the problem wasn't with the PeriodicExecutor function but rather my selector statement. So all I had to do was 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 is always the last thing that you look at.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »