Wicket 无法停止使用AbstractAjaxTimerBehavior创建的计时器
我正在编写一个单页wicket应用程序。当用户单击导航项时,将打开一个新选项卡。要求有两种类型的面板,一种是普通面板,另一种是自动刷新面板 我有一个TabPanel类,它从Panel扩展而来,Panel实际上包含了所有选项卡和管理选项卡的方法,如add、remove、set current 下面是我添加到TabPanel的客户计时器Wicket 无法停止使用AbstractAjaxTimerBehavior创建的计时器,wicket,wicket-6,wicket-1.6,Wicket,Wicket 6,Wicket 1.6,我正在编写一个单页wicket应用程序。当用户单击导航项时,将打开一个新选项卡。要求有两种类型的面板,一种是普通面板,另一种是自动刷新面板 我有一个TabPanel类,它从Panel扩展而来,Panel实际上包含了所有选项卡和管理选项卡的方法,如add、remove、set current 下面是我添加到TabPanel的客户计时器 private class AutoRefreshTimer extends AbstractAjaxTimerBehavior { public Auto
private class AutoRefreshTimer extends AbstractAjaxTimerBehavior {
public AutoRefreshTimer(Duration updateInterval) {
super(updateInterval);
}
@Override
protected void onTimer(AjaxRequestTarget target) {
Tab currentTab = getCurrentTab();
if(currentTab != null) {
if(currentTab.getPanel() instanceof AutoRefreshPanel) {
AutoRefreshPanel ajaxRefreshPanel = (AutoRefreshPanel) currentTab.getPanel();
ajaxRefreshPanel.onRefresh(target);
}
}
}
}
下面是添加计时器“autoRefreshTimer”的方法,它是TabPanel类中的一个实例变量
private void addNewAutoRefreshTimer(final Duration autoRefreshInterval) {
autoRefreshTimer = new AutoRefreshTimer(autoRefreshInterval);
add(autoRefreshTimer);
}
以下是控制计时器的方法:
public void startAutoRefreshTimer(final AjaxRequestTarget target, final Duration autoRefreshInterval) {
if(!autoRefreshTimer.isStopped())
addNewAutoRefreshTimer(autoRefreshInterval);
autoRefreshTimer.restart(target);
}
public void stopAutoRefreshTimer(final AjaxRequestTarget target, final Duration autoRefreshInterval) {
if(autoRefreshTimer.isStopped())
addNewAutoRefreshTimer(autoRefreshInterval);
autoRefreshTimer.stop(target);
}
public void toggleAutoRefreshTimer(final AjaxRequestTarget target){
Tab currentTab = getCurrentTab();
if(currentTab != null) {
if(currentTab.getPanel() instanceof AutoRefreshPanel) {
AutoRefreshPanel ajaxRefreshPanel = (AutoRefreshPanel)currentTab.getPanel();
this.autoRefreshInterval = ajaxRefreshPanel.getAutoRefreshInterval();
startAutoRefreshTimer(target, this.autoRefreshInterval);
return;
}
}
stopAutoRefreshTimer(target, this.autoRefreshInterval);
}
当用户在选项卡之间切换时,计时器在“我的本地服务器”中启动和停止,或者打开新选项卡,或者切换到“已打开”,或者关闭当前选项卡(选择上一个选项卡),或者关闭另一个选项卡而不从当前选项卡切换。
但是,当我在远程位置的登台服务器中测试相同内容时,计时器在以下场景中失败
以下是在Remote中创建问题的步骤:
1. Open two Normal tabs.
2. Open a tab containing AutoRefreshPanel.
3. Close 1st or 2nd normal tab by keeping the tab containing AutoRefreshPanel active/current. This expect the timer to be
fired/running.
4. Switch to the remaining Normal tab. This stops the timer in local server but not in Remote.
除上述所有其他场景中的步骤外,计时器在本地和远程环境中均按预期启动和停止
//更新:我找到了此问题的修复程序。但不确定这是不是最好的。以下是我所做的更改:
private class AutoRefreshTimer extends AbstractAjaxTimerBehavior {
public AutoRefreshTimer(Duration updateInterval) {
super(updateInterval);
}
@Override
protected void onTimer(AjaxRequestTarget target) {
Tab currentTab = getCurrentTab();
if(currentTab != null) {
if(currentTab.getPanel() instanceof AutoRefreshPanel) {
AutoRefreshPanel ajaxRefreshPanel = (AutoRefreshPanel) currentTab.getPanel();
ajaxRefreshPanel.onRefresh(target);
return; // added return
}
}
stop(target); // added a call to stop method.
}
}
// Replaced startAutoRefreshTimer, stopAutoRefreshTimer and toggleAutoRefreshTimer with the following method:
public void manageAutoRefreshTimer(final AjaxRequestTarget target){
Tab currentTab = getCurrentTab();
if(currentTab != null) {
if(currentTab.getPanel() instanceof AutoRefreshPanel) {
AutoRefreshPanel ajaxRefreshPanel = (AutoRefreshPanel)currentTab.getPanel();
this.autoRefreshInterval = ajaxRefreshPanel.getAutoRefreshInterval();
if(!autoRefreshTimer.isStopped())
addNewAutoRefreshTimer(autoRefreshInterval);
autoRefreshTimer.restart(target);
}
}
}
我觉得奇怪的是,在
start
和stop
方法中,您都向组件添加了行为的新实例。这将添加几个计时器行为。我尝试了删除(行为)方法,但出现错误。