GWT计时器取消不工作 [英] GWT Timer cancel not working

查看:136
本文介绍了GWT计时器取消不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用GWT和GWTQuery写一个代码来区分点击和双击。我得到了这里的想法。所以我把它翻译成GWT像这样:(我的应用程序不能有全局变量,所以我做这一部分与元素属性):

I am trying to write a code to differentiate between simgle and double clicks using GWT and GWTQuery. I got the idea here. So I translated it into GWT like this: (my app can't have global variables, so I am doing that part with element attributes instead):

$("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毫秒内),应该会弹出双击。它适用于单击,但双击,即使双击警告弹出,单击确定以摆脱它,我发现

Here, when the image is clicked, an alert should pop up showing Single Click, and if the user double clicks (within 200 ms), it should pop up Double Click. It works fine for the single click, but on double click, even though the Double Click alert pops up, on clicking Ok to get rid of it, I find the Single Click alert waiting to be get rid of.

不知为什么,我认为 t.cancel()不是双击触发。任何人都可以告诉我如何解决这个问题?

Somehow, I think the t.cancel() is not firing on double-click. Can anyone tell me how to fix this?

更新:

接受的解决方案适用于警报,但当我需要事件参数时,它必须稍微调整。之后,问题又回来了,定时器未清除,现在我收到两个提醒,双击点击 ..

The accepted solution works fine for alerting, but when I need the event parameter as well, it has to be slightly tweaked. After doing that, again, the problem comes back, the timer is not cleared, and now I get two alerts, Double Click 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更新:根据我的注释,最后一个代码应该是:

Updated by @Manolo: Based on my comment below last code should be:

      $("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;
        }
      });


推荐答案

以下代码与jsfiddle例如你说:

The following code does exactly the same than in the jsfiddle example you say:

  $("img").click(new Function() {
    boolean b = false;
    Timer t = new Timer() {
      public void run() {
        Window.alert("Click");
      }
    };
    public void f() {
      if (b = !b) {
        t.schedule(200);
      } else  {
        t.cancel();
        Window.alert("Double Click");
      }
     }
  });

防止默认片段不是必需的,除非你以前是sunk事件到元素,但在case ,代码应该是:

The prevent default fragment is not necessary unless you previously were sunk events to the element, but in the case, the code should be:

  $("img").dblclick(new Function() {
    public boolean f(Event e) {
      return false;
    }
  });

已编辑
如果您希望每个匹配元素,最好的方法是创建与元素一样多的函数。使用 GQuery.each()是更容易的方法:

  $("img").each(new Function() {
    public void f() {
      $(this).click(new Function() {
        boolean b = false;
        Timer t = new Timer() {
          public void run() {
            Window.alert("Click");
          }
        };
        public void f() {
          if (b = !b) {
            t.schedule(200);
          } else  {
            t.cancel();
            Window.alert("Double Click");
          }
         }
      });
  }});

您甚至可以只使用一个函数并在元素中存储变量,计数器,如你在你的查询中假装,但每个元素的计时器:

You could even use only one function and store variables in the element, but you should store not only the counter like you pretend in your query but a timer per element as well:

  $("img").click(new Function() {
    public void f() {
      boolean b = $(this).prop("c", Boolean.class);
      Timer t = $(this).prop("f");
      if (t == null) {
        t = new Timer() {
          public void run() {
            Window.alert("Click");
          }
        };
        $(this).prop("f", t);
      }
      if (b = !b) {
        t.schedule(200);
      } else  {
        t.cancel();
        Window.alert("Double Click");
      }
      $(this).prop("c", b);
     }
  });

这篇关于GWT计时器取消不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆