jQuery Spy improvements
I've made a diff against spy 1.4 to make sure it will not allow multiple spy instances on the page with the same object ID. If .spy() is called again, the old timer will be cleared and the new one (with new settings) will take over. It should still allow multiple *different* spies on the same page, just not two of the same thing (I was having an issue where it would keep reloading the same ID over and over because of a .click() event changing the configuration settings) - this allows real-time changing of the settings (say, the AJAX URL) without spawning additional timers.
I also added in a Math.random() parameter to force reloads every call and changed $.post to $.get - those can easily be removed if desired. 🙂 Essentially I just create an array that has a list of all the spy IDs that are called, if a dupe is detected, the old timer is disabled and the new one starts like normal. Oh, and I changed the epoch behavior, for some reason on my browser it wasn't reporting the right time. I don't see why it needed the spy.epoch calculation at all.
Here's the patch file:
spy-1.4-diff.patch.txt
Feel free to submit your comments. This was the cleanest method I could figure out.
Hi,
Ive applied the patch, but im not sure how to change the application.
Could you please apply an example of usage?
Thanks!
Actually it's been a while since we used this. We did away with that interface a while ago. I am not sure how well this will work but this appears to be the last version we used. Hopefully this points you in some sort of path...
$(document).ready(function() {
// prefill on first load
$.get('/data/json.php?f=list&p2=25&r='+Math.random(), function(data) {
var jdata = eval('('+data+')');
for(i=0; i div:gt(20)').fadeEachDown();
});
// start spy
$('#entriesMain').spy({ 'limit': 25, 'fadeInSpeed': 1400, 'timeout': 15000, 'fadeLast': 5, 'ajax': '/data/json.php?f=list', 'push': display, 'method': 'json', 'isDupe': isDupe });
});
function myTimestamp() {
var d = new Date();
return Math.round(d.getTime() / 1000);
}
function isDupe(l, p) { return (l.entry_id == p.entry_id); }
function display(r, f) {
var html = '';
html += 'ID: '+r.entry_id + ' ';
html += r.entry_title;
html += r.entry_description;
html += r.entry_submitted + '';
if(f) {
$('#entriesMain').append(html);
} else {
$('#entriesMain').prepend(html);
}
}