Java – how to correctly delete ajaxselfupdatingtimerbehavior from components in Apache wicket?
I had trouble adding and removing ajaxselfupdating timerbehavior in Apache wicket The behavior is added, but once I delete the behavior, I will soon appear "page expired" in the browser. I think it is because the deletion is not clean My setup is basically a tag, which starts to change through a timer, and there are two links: "go" and "stop" I want to be able to click "go" and "stop" (obviously I know it will never work the opposite way!) This is my complete tag:
<html> <body> <span wicket:id="message">message will be here</span><br/> <a wicket:id="go">Go</a><br/> <a wicket:id="stop">Stop</a> </body> </html>
This is my code:
// imports all from standard wicket public class HomePage extends WebPage { private static final int INTERVAL = 500; public HomePage(final PageParameters parameters) { final Component label = new Label("message","Hello").setOutputMarkupId(true); add(label); final IBehavior updater = new AjaxSelfUpdatingTimerBehavior(Duration .milliseconds(INTERVAL)) { @Override protected void onPostProcessTarget(AjaxRequestTarget target) { label.setDefaultModelObject(String.valueOf(System.nanoTime())); } }; AjaxLink<String> go = new AjaxLink<String>("go") { @Override public void onClick(AjaxRequestTarget target) { label.add(updater); target.addComponent(label); } }; AjaxLink<String> stop = new AjaxLink<String>("stop") { @Override public void onClick(AjaxRequestTarget target) { label.remove(updater); target.addComponent(label); } }; add(go,stop); } }
I'm using wicket 1.4 three
Any help would be appreciated thank you.
Solution
I solved this problem by using the stop () method instead of trying to completely delete the behavior
I really want to completely delete it at some point after stopping it (because my solution involves creating a new behavior every time I click "go", I want to continue to stop and start without generating a million behaviors), so I get to maintain a list of behaviors that go back and forth later