• Post Reply Bookmark Topic Watch Topic
  • New Topic

Display relative time on a web page - How would you do it?

 
Stamin Adrian
Ranch Hand
Posts: 34
Chrome Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Picture you have a website that has comments on. Your website is online and running but everytime a minute passes your comments get older and they change from this: "3 minutes ago" to this " 5 minutes ago" , "one hour ago" etc.
How would you go about doing that? Would you spawn a thread? I'm not really sure how I can start just one thread when the app is deployed, or when it aknowledges that the database has comments. Any help appreciated!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Threads? No.

I'd just use a JavaScript timer on the page.
 
Stamin Adrian
Ranch Hand
Posts: 34
Chrome Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I learned how to start threads with executor the right way.One harmless thread might be ok. I thought about javascript, but then the web page may not be opened all the time. Time still pases by. The server is running most of the time. I'm going to go with threads, just for the sake of learning about them more.

Thanks for the response Bear!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the web page isn't displayed, what is there to update? How will this thread "write" to the page? Unless there's something I missing you're trying to use a melon baller to drive a screw.
 
Stamin Adrian
Ranch Hand
Posts: 34
Chrome Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will update the database.
 
Stamin Adrian
Ranch Hand
Posts: 34
Chrome Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought of it this way: One thread keeps ageing the comments, no matter if website is viewed or not. On the client side one javascript timer does an ajax request from time to time to get the age from the db.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why? You can simply calculate a comment's age at the time that a page that contains it is requested. Once that's happened, and you want to keep a comment's age up-to-date without the user having to reload the entire page, you can use JavaScript to do that. Honestly there is no need to persistenly keep track of a comment's age, and have a dedicated thread to update it. It can be calculated at all times based on the comment's timestamp i.e. when it was posted (which you will have to store) and the current time.
 
Stamin Adrian
Ranch Hand
Posts: 34
Chrome Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. But all this timezone stuff seems complicated. Yes, you do have a good point. Ty.
 
Tim Holloway
Bartender
Posts: 18417
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot run a server thread to update.

The Web is neither asynchronous nor continually-connected.

If a web page wants to be updated, it has 2 choices:

1. Local javascript code

2. Make a request to the server. The server will then respond with the update back to the requester and disconnect until the next request.

I myself would recommend running the update process as a local JavaScript, because it's less of a load on the server for trivial work and because it won't be subject to delays and irregular response times.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fact that you want to update how old a comment is probably means (I could be very wrong of course) that you are soon going to need to be able to show new comments when they are available without the user needing to refresh as well. If your application is driven by many such requirements of the server notifying the client for updates then you should consider adding full duplex communication with websockets to the application.
 
Stamin Adrian
Ranch Hand
Posts: 34
Chrome Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know much about websockets, but my application is polling the database for new comments with ajax (setInterval(function(), time)), so no need for refresh there. I slaved at that thing for days. I will probably just date them, like javaranch is doing it, and present it for the users specific timezone. Thanks for helping!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!