gringogordo
gringogordo

Reputation: 2140

web app - perform an action 90 seconds after navigating away from the home page

I have a java (7) web app (spring mvc). The business have asked for an action (display a pop up announcement) to take place 90 seconds after the user navigates away from the home page. The views/pages are all jsp pages and all of them have a header and footer jspf. There are not too many web pages in the application - maybe about 8 (it being more about the data).

I'm looking for suggestions about the best way to do this! I'm thinking adding a session date/time value in the unload event and then checking against this value every 4 or 5 seconds (the 90 seconds isn't mission critical!) until I hit 90 seconds.

What strikes me most about this solution is although it probably won't affect the user experience too much it is a bit inefficient as I can't see how I will be able to stop checking the session variable every 5 seconds even once the action has been completed.

Could anyone help me with a better approach ?

Upvotes: 0

Views: 67

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 339737

This Answer is for a server-side Java solution, where you have Push available. Ideal for server-side framework like Vaadin but may be overkill for simple JSP page.

Executor

Java 5 and later bundles sophisticated classes for scheduling tasks on background threads. These classes implement Executor.

Specifically, the ScheduledExecutorService. You specify a number such as 90 and a time unit such as Seconds. You pass a Callable or Runnable object as the task to be performed. Java handles the rest. You get back a ScheduledFuture which you can query for its status if you need to monitor completion.

See the Tutorial.

This work happens on a background thread. So your Callable/Runnable needs to reach the main Servlet’s thread and its objects so you must be aware of concurrency issues.

All of this has been covered extensively on StackOverflow. So search for further info.

Upvotes: 1

Michael Lorton
Michael Lorton

Reputation: 44416

Don't forget: the current page has control of the Javascript. Any pending timeouts are simply discarded when the browser leaves a page.

So the page you go to has be the one with the actual alert.

One way might be for the unload() function of the home page to write something in a cookie or in local-storage that says, "At this specific time [90 seconds in the future], display this message."

Then every page (probably the header of every page) has to look for the something, and if it is there, and the time is still in the future, set a timeout for that many seconds; when the timeout expires, pop up the message and erase the something.

Upvotes: 0

Related Questions