我正在尝试编写代码来区分使用 GWT 和 GWTQuery 的单击和双击。我明白了。所以我把它翻译成这样的 GWT:(我的应用程序不能有全局变量,所以我用元素属性来做那部分):
$("img").live("click", new Function() {
public boolean f(Event event) {
String clicksString = $(event).attr("clicks");
int clicks = Integer.parseInt(clicksString);
clicks++;
$(event).attr("clicks",String.valueOf(clicks));
Timer t = new Timer() {
@Override
public void run() {
Window.alert("Click");
$(event).attr("clicks","0");
}
};
if (clicks == 1) {
t.schedule(200);
}
else {
t.cancel();
$(event).attr("clicks","0");
Window.alert("Double Click");
}
return true;
}
});
在这里,当单击图像时,应该会弹出一个警报,显示Single Click,如果用户双击(在 200 毫秒内),它应该会弹出Double Click。单击它可以正常工作,但是在双击时,即使弹出双击警报,单击确定以摆脱它,我发现单击警报等待被摆脱。
不知何故,我认为t.cancel()
双击时不会触发。谁能告诉我如何解决这个问题?
更新:
公认的解决方案可以很好地用于警报,但是当我也需要该event
参数时,必须对其进行微调。这样做之后,问题又回来了,计时器没有清除,现在我收到两个警报,双击和单击..:
$("img").live("click", new Function() {
int clicks = 0;
public boolean f(Event event) {
Timer t = new Timer() {
@Override
public void run() {
clicks = 0;
// Here I need the event paramater
}
};
if (clicks++%2 == 0) {
t.schedule(5000);
}
else {
t.cancel();
clicks = 0;
// Here also I need the event paramater
}
return true;
}
});
@Manolo 更新:根据我在下面的评论,最后的代码应该是:
$("img").live("click", new Function() {
int clicks = 0;
Event event;
Timer t = new Timer() {
@Override
public void run() {
// Use the event
event....
}
};
public boolean f(Event event) {
this.event = event;
if (clicks++%2 == 0) {
t.schedule(5000);
} else {
t.cancel();
// Use the event
event....
}
return true;
}
});