我正在尝试编写代码来使用 GET 和 GETQuery 区分单击和双击。我明白了here http://jsfiddle.net/KpCwN/4/。所以我将它翻译成 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;
}
});
在这里,当单击图像时,会弹出一个警报,显示单击,如果用户双击(200毫秒内),它应该弹出双击。单击时效果很好,但双击时,即使双击单击时会弹出警报Ok为了摆脱它,我找到了单击警觉等待被摆脱。
不知何故,我认为t.cancel()
双击时不会触发。谁能告诉我如何解决这个问题?
Update:
接受的解决方案适用于警报,但是当我需要时event
参数也一样,需要稍微调整一下。这样做之后,问题再次出现,计时器没有被清除,现在我收到两个警报,双击 and Click..:
$("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;
}
});