i want to be able to update the time values of all comments on a page as in the time elapsed.
i know that i should be running a script with setInterval()
or may be use long polling, don't know if this is a preferred technique.
Moreover, even if i get the updated values(maybe in an array) of all comments in JS response, how am i supposed to assign the values to related comments.
If there is some other logic i should use, please suggest and help with it.
I think it would be more or less like push notification functionality.
*EDIT: * since this has been downvoted already, but also that i have now posted the question, i would appreciate if people would suggest me something please..
update: I achieved the above functionality by recreating the PHP function which calculated elapsed time in JavaScript which is as follows:
function calcTime(date){
if(date == ""){
return "No date provided";
}
periods = new Array("second", "minute", "hour", "day", "week", "month", "year", "decade");
lengths = new Array("60","60","24","7","4.35","12","10");
now = Math.round(new Date().getTime() / 1000);
unix_date = date;
// check validity of date
if(unix_date == ""){
return "bad date";
}
// future date or past date
if(now > unix_date){
difference = now - unix_date;
tense = "ago";
} else {
difference = unix_date - now;
tense = "from now";
}
for(var j=0;difference >= lengths[j] && j < (lengths.length) - 1; j++){
difference /= lengths[j];
}
difference = Math.round(difference);
if(difference != 1){
periods[j] += "s";
}
return difference+" "+periods[j]+" "+tense ;
}
in the HTML returned a hidden value with the timestamp from database and then looped through the results in JS to get the new values using setInterval()
function, thereby eliminating the need to call php and unnecessary initiating ajax requests. something like this:
(function(){
setInterval(function (){
timeValues = document.getElementsByClassName('unixTime');
abbrValues = document.getElementsByClassName('full_timestamp');
len = timeValues.length;
alen = abbrValues.length;
for(i=0; i<len;i++){
ut = timeValues[i].value;
for(j=0;j<alen;j++){
abbrValues[i].innerHTML = calcTime(ut);
}
}
},10000);
})();
Thanks everyone who tried to help and i appreciate the moderators for reopening this question.