Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
Detecting if navigator.vibrate can be called

I noticed recently an error popup in Chrome telling that navigator.vibrate can only be called once the user interacted with the page:

Chrome vibrate error popup

I could change my page to ask for user interaction (with a button for example) but I'd rather keep my page as is. But then I would need know if I can call navigator.vibrate.

My first approach was set a boolean to true after the first touchstart event:

let canVibrate = false;
document.addEventListener("touchstart", () => {
    if (canVibrate) {
        navigator.vibrate(200);
    } else {
        canVibrate = true;
    }
});

This first approach works when the user touches with one finger, so probably most of the cases.

However this doesn't work if before pulling of its finger, the user puts down another. In that scenario, the error pops up for every touch done before removing all fingers. And then, it still needs one more touch, as if all these didn't count as an interaction.

Is there any javascript function that lets me know if navigator.vibrate can be called? And if not what are some ways to test it?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As per an example on the MDN page for touch events

This calls event.preventDefault() to keep the browser from continuing to process the touch event (this also prevents a mouse event from also being delivered). Then we get the context and pull the list of changed touch points out of the event's TouchEvent.changedTouches property.

To put this in an example you can do something like:

let canVibrate = false;
document.addEventListener("touchstart", (event) => {
    if (!canVibrate) {
        canVibrate = true
    }
    navigator.vibrate(200);
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!