Web Weather > Weather Website PHP/AJAX scripting
ajaxWDwx.js Limit to number of Updates of Clientraw data.
(1/1)
TNETWeather:
After adding Ajax to my pages, I noted a large increase in traffic in the logs...
The traffic increase was the updates of clientraw data for the Ajax code. Since I have it set to update once every 30 seconds, anyone that camped out on a page with it, would make 120 downloads of the clientraw data per hour.
Not a real big deal as the bandwidth is relatively low (~660 bytes x 120 = 80k an hour).
However, I did start noticing that some "visitors" were camped out for hours even days at a time. Most likely sitting in a tab they are not viewing (many of them sitting on my Scripts page for example). One visitor appears to have been there now for 4 days...
So I thought about adding update limits to the Ajax code. WDL has the same type of restriction ability and most that use it are much more restrictive than 2 hours.
I added at the top of the ajaxWDwx.js file...
var updates = 0; // Start counter
var maxupdates = 240; // Maxium Number of updates allowed
This setup two variable... updates which would be incremented each time an update took place, and maxupdates. I set it initially to 5 so I could test it without waiting two hours... but now have it at 240 which is 2 hours using my update rate of once very 30 seconds.
Next I modified the line which checked if a valid clientraw was found... to:
// If we have a valid clientraw file AND updates is < maxupdates
if(clientraw[0] == '12345' && wdpattern.test(x.responseText) && updates < maxupdates ) {
updates ++;
This basically says that if updates is < maxupdates go ahead if the other conditions are met... followed by an increment of the updates variable if it did get past the if statement.
Lastly, I changed the following lines:
setTimeout("reset_ajax_color('')",flashtime); // change text back to default color
setTimeout("ajaxLoader(clientrawFile + '?' + new Date().getTime())", reloadTime); // get new data after 5 secs
to:
if (updates < maxupdates) {
setTimeout("reset_ajax_color('')",flashtime); // change text back to default color
setTimeout("ajaxLoader(clientrawFile + '?' + new Date().getTime())", reloadTime); // get new data after 5 secs
}
This prevents any update of the timeouts if the updates are now larger than the maxupdates.
Ideally, I would do some more like:
o Allow for 0 maxupdates to mean no limit
o If paused, update somewhere where it indicates that the data is now frozen.
saratogaWX:
I'm doing a new release of the WD/AJAX/PHP sample set, and the ajaxWDwx.js V2.09 will include this nifty feature, along with the 0=no limit, and a replacement for the ajaxindicator text with 'Updates paused - reload page to start',
and a bunch of other enhancements too.. Should be ready for release tomorrow :-)
Thanks for the idea and demo code, Kevin!
Best regards,
Ken
Navigation
[0] Message Index
Go to full version