Win a flower (🌹) or copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java (📚) this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
master stewards:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
garden masters:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
gardeners:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

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

 
pioneer
Posts: 34
Netbeans IDE Tomcat Server Chrome
  • 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!
 
Marshal
Posts: 67357
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
pioneer
Posts: 34
Netbeans IDE Tomcat Server Chrome
  • 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
Marshal
Posts: 67357
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
pioneer
Posts: 34
Netbeans IDE Tomcat Server Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will update the database.
 
Stamin Adrian
pioneer
Posts: 34
Netbeans IDE Tomcat Server Chrome
  • 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.
 
gardener
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
pioneer
Posts: 34
Netbeans IDE Tomcat Server Chrome
  • 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.
 
gardener
Posts: 21719
148
Android Eclipse IDE Tomcat Server Redhat Java 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.
 
pollinator
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
pioneer
Posts: 34
Netbeans IDE Tomcat Server Chrome
  • 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!
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!