使用setInterval导致事件的多重绑定问题

使用定时器常常会出现一些时间紊乱的问题,之前我就遇到过使用setInterval产生动画,当一中断事件不断的被触发,这个就很可能会导致时间紊乱、动画紊乱的问题,想必大家在也都经历过这样的问题,解决这个问题的方法是,构造在某时刻只存在一条时间线的机制,延迟事件触发,这个将在以后的文章中讨论,今天发现了一个问题,使用setInterval会导致同一绑定的多重触发,代码如下
<script language='javascript' src='jquery.js'></script>
<script language='javascript'>
$(document).ready(function(){
function tt(){
//...一些动画代码
$(window).keydown(function(event){
if(event.keyCode == 37) alert("yes");
});
}
setInterval(tt,1000);
});
</script>

如果等1秒,按Left键,会出现1个alert,停留5秒,再按一次Left键,会出现5个alert,也就是,每一秒注册一个监听器似的,解决方法很简单,将keydown绑定事件不参与setInterval就行了,直接和setInterval平级,即:
$(document).ready(function(){
function tt(){
//...一些动画代码

}
$(window).keydown(function(event){
if(event.keyCode == 37) alert("yes");
});
setInterval(tt,1000);
});
展开阅读全文

JTree 多重事件 问题

05-27

jtree 刷新, rnrn都无效。且出异常rnrn该tree 是从用 Resutlet 来填充的。 对其删除已实现,但是 删除后的刷新 就出异常rnrn刷新在 鼠标右键弹出式菜单中(popupMenu) 中写的。 每次触发 popupMenu 的actionListener 的时候 就爆错。rnrn我在刷新的实现还是是通过ResultSet 来填充,但是无效果。rnrn后来直接用下班的方法,无果且 报错。 rntreeModle.reload();rnjtree1.updateUI();rnrnrnrnrn我的树 是这么样的rnrn 鼠标左键来过滤数据(点击该节点) 会在右边的table 里显示 该 节点对应的数据。(valueChange事件中完成)rnrn 但是我刷新的时候由于节点是已选中的 就出以下的错rn 如果节点没有选择 不会执行 刷新的。rnrnrnrn[code=Java]rnrnException in thread "AWT-EventQueue-0" java.lang.NullPointerExceptionrn at paysys.frame.JF_ItemCode$2.valueChanged(JF_ItemCode.java:597)rn at javax.swing.JTree.fireValueChanged(JTree.java:2399)rn at javax.swing.JTree$TreeSelectionRedirector.valueChanged(JTree.java:2770)rn at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:629)rn at javax.swing.tree.DefaultTreeSelectionModel.clearSelection(DefaultTreeSelectionModel.java:570)rn at javax.swing.tree.VariableHeightLayoutCache.rebuild(VariableHeightLayoutCache.java:740)rn at javax.swing.tree.VariableHeightLayoutCache.treeStructureChanged(VariableHeightLayoutCache.java:626)rn at javax.swing.plaf.basic.BasicTreeUI$Handler.treeStructureChanged(BasicTreeUI.java:3660)rn at javax.swing.tree.DefaultTreeModel.fireTreeStructureChanged(DefaultTreeModel.java:559)rn at javax.swing.tree.DefaultTreeModel.reload(DefaultTreeModel.java:277)rn at javax.swing.tree.DefaultTreeModel.reload(DefaultTreeModel.java:194)rn at paysys.frame.JF_ItemCode.actionPerformed(JF_ItemCode.java:763)rn at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)rn at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)rn at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)rn at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)rn at javax.swing.AbstractButton.doClick(AbstractButton.java:302)rn at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1000)rn at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1041)rn at java.awt.Component.processMouseEvent(Component.java:5488)rn at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)rn at java.awt.Component.processEvent(Component.java:5253)rn at java.awt.Container.processEvent(Container.java:1966)rn at java.awt.Component.dispatchEventImpl(Component.java:3955)rn at java.awt.Container.dispatchEventImpl(Container.java:2024)rn at java.awt.Component.dispatchEvent(Component.java:3803)rn at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)rn at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)rn at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)rn at java.awt.Container.dispatchEventImpl(Container.java:2010)rn at java.awt.Window.dispatchEventImpl(Window.java:1774)rn at java.awt.Component.dispatchEvent(Component.java:3803)rn at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)rn at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)rn at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)rn at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)rn at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)rn at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)rnrnrnrn[/code]rnrnrnrn求解。。。。。 论坛

没有更多推荐了,返回首页