Skip to main content

Imitate Long Press / Taphold On Desktop With jQuery

By May 22, 2017Blog, Javascript, jQuery
Allure Web Solutions Code Snippet

In this blog post I show how to imitate a long press/ taphold on the desktop. There’s not really any use cases for this that I can think of in the real world. The same technique could be applied to some HTML5 game I guess.

The JavaScript

(function(window, jQuery) {
  var $body = $("body"), $touchArea = $("#toucharea");

  $body
    .on("mouseup", $touchArea, function(e) {
      clearTimeout(pressTimer);
    
      return false;
    })
    .on("mousedown", $touchArea, function(e) {
      // Set timeout to imitate 1s press
      pressTimer = window.setTimeout(function() {
        
        // Perform some action after 1s press
        doAction(e);
      }, 1000);
      return false;
    });

  function doAction(e) {
    // do something 
  }
})(window, jQuery);
// Credits: https://stackoverflow.com/a/2625240/3602355

Demo

See the Pen Imitate Long Press / Taphold On Desktop With jQuery by Mike Doubintchik (@allurewebsolutions) on CodePen.

allure

Author allure

More posts by allure

Leave a Reply

Designed by

best down free | web phu nu so | toc dep 2017