Frontend Development 8 min read

Implementing Long‑Press Functionality in Vue with JavaScript

This article explains how to create a long‑press button in a Vue application by using native JavaScript events, timers, and a custom Vue directive, covering variable setup, start and cancel functions, event listeners for mouse and touch, and error handling for non‑function bindings.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
Implementing Long‑Press Functionality in Vue with JavaScript

The article demonstrates how to add a long‑press capability to a Vue component, allowing a function to be executed when a user holds a button for a specified duration.

To achieve this, the mousedown event starts a timer and the mouseup (or click ) event cancels it; if the timer reaches the threshold (e.g., 2 seconds), the associated function runs.

Implementation steps include defining a variable to store the timer, creating a start function that sets a setTimeout , and a cancel function that clears the timeout.

Key code snippets:

let pressTimer = null;
let start = (e) => {
  if (e.type === 'click' && e.button !== 0) { return; }
  if (pressTimer === null) {
    pressTimer = setTimeout(() => {
      // execute task !!!
    }, 1000);
  }
};
let cancel = (e) => {
  if (pressTimer !== null) {
    clearTimeout(pressTimer);
    pressTimer = null;
  }
};

These functions are attached to the target element:

el.addEventListener("mousedown", start);
el.addEventListener("click", cancel);
el.addEventListener("mouseout", cancel);

For Vue integration, a global custom directive v-longpress is defined. Inside the bind hook, the same timer logic is set up, a handler function invokes the user‑provided callback, and additional listeners for touchstart , touchend , and touchcancel are added to support touch devices.

Vue.directive('longpress', {
  bind: function(el, binding, vNode) {
    if (typeof binding.value !== 'function') {
      const compName = vNode.context.name;
      let warn = `[longpress:] provided expression '${binding.expression}' is not a function, but has to be `;
      if (compName) { warn += `Found in component '${compName}' `; }
      console.warn(warn);
    }
    let pressTimer = null;
    let start = (e) => { /* timer start logic */ };
    let cancel = (e) => { /* timer cancel logic */ };
    const handler = (e) => { binding.value(e); };
    el.addEventListener('mousedown', start);
    el.addEventListener('touchstart', start);
    el.addEventListener('click', cancel);
    el.addEventListener('mouseout', cancel);
    el.addEventListener('touchend', cancel);
    el.addEventListener('touchcancel', cancel);
  }
});

Finally, the directive validates that the bound expression is a function and logs a warning if not, ensuring developers receive clear feedback during integration.

frontendJavaScriptVueDirectiveLong Press
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.