I am doing a live time stamp update the following way:
var refreshtimest;
function refreshtimes() {
var allt = '';
$("abbr[class=livetimestamp]").each(function () {
var did = $(this).attr("id");
did = did.replace("dtime", "");
var tit = $(this).data("utime");
allt += ':{}' + did + ',/:{}' + tit;
});
$.ajax({
async: "false",
type: "POST",
url: "refreshtimes.php",
data: {
alltimes: allt
},
success: function (response) {
if (response.length > 0) {
var res = response.split("<>");
var rres = res.length - 1;
var x = 0;
var id;
while (x <= rres) {
if (strpos(res[x], ',/') !== false) {
id = parseInt(res[x]);
} else if (id != "undefined") {
document.getElementById("dtime" + id).innerHTML = res[x];
}
x++;
}
}
}
});
refreshtimest = setTimeout("refreshtimes()", 60000);
}
refreshtimest = setTimeout("refreshtimes()", 2000);
OK, that is the javascript, now the parse of the retrieved string sends:
5,/<>36 minutes ago<>2,/<>September 5 at 11:52am<>3,/<>September 5 at 11:48am<>72,/<>September 4 at 4:43pm<>73,/<>September 4 at 3:29pm<>33,/<>September 4 at 3:08pm<>16,/<>September 4 at 2:19pm<>70,/<>September 4 at 2:46pm<>0,/<>September 4 at 3:20pm<>4,/<>September 4 at 3:22pm<>71,/<>September 3 at 1:04pm<>6,/<>September 3 at 3:20pm<>7,/<>September 3 at 3:18pm<>8,/<>September 3 at 3:17pm<>9,/<>September 3 at 3:14pm
My problem is I need to set ids, the response gives 5,/<>readable format of time
whereas 5 is the id to update the html
I would like to update the html without knowing the id but keeping one post only to the server,
example
selecting all abbr with class livetimestamp and sending an ajax call for each is unproductive, making a single string sounds better, in this string as you see from the above js code it sends the id as var did, along with the data, data(“utime”).
I would like to start sending just data from each abbr with a class livetimestamp but be able somehow to have these identified with no id.
something like another loop for each class=livetimestamp but perform the update from the retrieved response – or apply to each selected from the first each assuming these had no id set.
Reason is I want to have
<abbr class="livetimestamp" data-utime"xxxx">html</abbr>
And no
<abbr class="livetimestamp" data-utime"xxxx" id="words_numbers">html</abbr>
it’s just unproductive on the server to keep setting ids be it whether they must be unique or because a counter has to be set up and these counter shouldn’t get mixed with different sections that need a livetimestamp class as well. so to have it working with the class only would be great.
Basically I don’t think you need to use the id’s in the first place..
When You use this selector
$("abbr[class=livetimestamp]")it parses the DOM in top-down order and all the elements are selected in a sequential manner..So instead of sending the ID of the elements you can send the Times itself as a comma separated sting , which are in sequential order.. Then refresh the times on the server and get back in the same format..