简体   繁体   English

在超时函数中触发click()事件

[英]Triggering a click() event in a timeout function

My user interface has a dual-use button that should open a small color palette when clicked, or a full RGB color selector (an HTML5 color input element) when held down for 500ms. 我的用户界面有一个两用按钮,可以在单击时打开一个小调色板,或者在按住500ms时打开一个完整的RGB颜色选择器(HTML5颜色输入元素)。

The implementation for such a button is straightforward: The mousedown event sets a timer that triggers fnHold when it expires; 这样一个按钮的实现很简单:mousedown事件设置一个计时器,当它到期时触发fnHold; the mouseup event clears the timer and triggers fnClick if the fnHold hasn't been triggered yet; mouseup事件清除计时器并触发fnClick如果尚未触发fnHold; the mouseleave event clears the timer and does nothing. mouseleave事件清除计时器并且什么都不做。

My example code is here: http://jsfiddle.net/vwoof2a5/1/ (in this example, the button should do the same thing when it is clicked or held down). 我的示例代码在这里: http//jsfiddle.net/vwoof2a5/1/ (在这个例子中,按钮应该在单击或按下时执行相同的操作)。

  var mouseHold = function(element, duration, fnHold, fnClick) {
    var timeout = 0;
    var held = false;
    fnHold = fnHold.bind(element);
    fnClick = fnClick.bind(element);

    element.on({
      mousedown: function() {
        timeout = setTimeout(function() {
          held = true;
          fnHold();
        }, duration);
      },
      mouseup: function() {
        clearTimeout(timeout);
        if (!held) fnClick();
        held = false;
      },
      mouseleave: function() {
        clearTimeout(timeout);
        held = false;
      }
    });
  };


$(document).ready(function() {
  mouseHold($('#holder'), 500, function() {
    $('#log').append("<p>Hold event</p>");
    $('#color').click();
  }, function() {
    $('#log').append("<p>Click event</p>");
    $('#color').click();
  });
});

Unfortunately, this doesn't work in all browsers. 不幸的是,这并不适用于所有浏览器。 Chromium has no problem, but 铬没有问题,但是 Firefox doesn't open the color selector when the button is held. 按住按钮时Firefox不会打开颜色选择器。 It does run the timer function, but the $('#color').click() just doesn't go through. 确实运行了计时器功能,但$('#color')。click()只是不通过。

I'm not sure if this is a security feature (not allowing timer functions to trigger a click) or something other problem, and I don't know how to work around it. 我不确定这是否是一个安全功能(不允许定时器功能触发点击)或其他问题,我不知道如何解决它。 Is there another way to open the selector that doesn't depend on click()? 有没有其他方法可以打开不依赖于click()的选择器?

The timer could be avoided entirely by comparing times on mouseUp, but that forces users to decide how long to hold down the button, and is less convenient. 通过比较mouseUp上的时间可以完全避免计时器,但这会迫使用户决定按住按钮多长时间,并且不太方便。

Edit: Chromium doesn't do it either; 编辑:Chromium也没有这样做; the earlier result might have been a fluke. 早期的结果可能是侥幸。

Edit2: Chromium's reaction is confusing. 编辑2:Chromium的反应令人困惑。 As pointed out by James Montagne, the hold-event does not work the first time after the page has loaded or the first time after the regular click has been tested, but works normally thereafter. 正如詹姆斯·蒙塔涅指出,保持事件工作后的页面加载或首次常规点击后已经过测试,但此后正常工作的第一次。

I am not an expert on Events, but you may have more luck using $(selector).trigger('click') to trigger the click and $(selector).on('click') to define the handler. 我不是活动方面的专家,但是你可能有更多运气使用$(选择器).trigger('click')触发click和$(selector).on('click')来定义处理程序。

I generally use all jQuery or all raw JS so that these issues don't happen. 我通常使用所有jQuery或所有原始JS,以便不会发生这些问题。

I am interested if these work. 如果这些工作,我感兴趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM