Detecting Triple Taps on Mobile Devices

I've been trying to incorporate a triple-tap escape feature similar to The Trevor Project's Website. It functions flawlessly on laptops and desktops with a mouse. However, I'm struggling to detect the triple tap on mobile browsers because they interpret it as a double-tap after the first two taps, causing them to zoom in and miss registering the third tap. Despite attempting various implementations involving preventDefault() and setTimeout(), I haven't found a solution. I've spent countless hours researching and testing different fixes without success.

Just so you know, I am aware of the option to disable double-tap zoom using touch-action: manipulation in CSS, but this doesn't work on newer versions of Safari iOS, and I need this feature to be universally supported across all browsers.

Below is a snippet of the code before any attempts were made to resolve the issue. The click part works fine, but the tap version does not.

        window.addEventListener('click', function (event) {
            if (event.detail === 3) {
            window.location.replace("http://google.com");
            }
                });

        window.addEventListener('touchstart', function (event) {
            if (event.detail === 3) {
            window.location.replace("http://google.com");
            }
                });

I'm running out of ideas, can anyone provide a solution for this problem?

Answer №1

In order to maintain the integrity of all events, my recommendation is to refrain from modifying or adjusting them. Instead, focus on tracking the number of clicks/taps and resetting if the user takes too long during the third interaction. Here's an example of how the code could be structured:

let clickCount = 0;

// Display click count on screen
const clicksDisplay = document.getElementById("clicks");

function resetClicks(seconds){
  setTimeout(function(){
    clickCount = 0;
  },seconds*1000)
}

// Event listener for clicks (can be applied to any event listener)
window.addEventListener('click', function () {
  clickCount += 1;
  clicksDisplay.textContent = clickCount;
  if (clickCount === 3) {
    clickCount = 0;
    clicksDisplay.textContent = 'Third Click!';
  } else if(clickCount == 2){
    // Define the seconds to wait before resetting
    resetClicks(1);
  }
 //Example display text
  
});
<span id="clicks">0</span>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a Curved Border with Clip Path in CSS

Hey there! I'm currently attempting to add a speech bubble at the bottom left using clip-path, but I'm struggling to adjust the pixels just right to achieve the clear and exact look I want. Can someone provide suggestions on how to accomplish thi ...

Anchor elements appear to overlap when in hover state

I am experiencing a challenge with creating a dropdown for the navigation menu on my mobile view. The anchors are overlapping and my attempt to stack them on top of each other using display block and margin-bottom has not been successful as evidenced by th ...

Harness the power of Ionic by utilizing the same HTML template across various pages, while easily customizing the content

I need help with streamlining my Ionic app that has multiple pages with similar HTML structures but different content. Is there a way to use just one HTML file and dynamically fill in the content? Should I use a controller for each page, or is there a more ...

Setting the maximum length value in HTML dynamically

There is an input field with specific formatting restrictions, such as a maximum length of 6 characters and only accepting numeric values. On blur event, the 6-digit code "123456" is automatically formatted to "12-34-56" and the browser remembers this form ...

Utilizing Discord.js to enable a command for a particular channel

Running a Discord.js bot and struggling to figure out how to restrict the command! help to only the #commands channel. Already have the channel ID, so what steps should be taken in the command event to achieve this? Appreciate any guidance! ...

Ignoring an 'if' statement while trying to set UITableView sections

I've encountered a problem where an 'if' statement seems to be ignored when setting the title for a section with multiple UITableViews. The issue is that both UITableViews are displaying 'Switzerland' instead of different titles. B ...

What is the best way to ensure that the item beneath two images is always centered in HTML5?

When designing a webpage with text and images, it's important to consider the layout on different screen sizes. To avoid text wrapping under images, adjusting the spacing around the image is sometimes necessary. Similarly, when embedding a video, cen ...

Using JavaScript to delete text and send data to the server

<script type="text/javascript"> function removeLink() { document.getElementById("tab2").deleteRow(i); } </script> </head> <body> <form action="forth.php" method="post"> <table width="600" border="1" id="tab2"> &l ...

Class remains unaffected by media query modification

With Bootstrap5 in play and an additional stylesheet added as the last one in the lineup, my goal is to customize the margins of the .content class. The snippet of code below resides at the end of my stylesheet... .content { width: 100vw; m ...

I am interested in extracting information from the Firebase real-time database and showcasing it on my HTML webpage

I am struggling to display data from the Firebase real-time database on my HTML page. Even though I can see all the data perfectly in the console, it doesn't show up on the webpage. I attempted to use a for loop, but it still doesn't display the ...

Unlocking the potential of Bootstrap 4 pop ups and modals within an Angular 6 project

Angular.json "styles": [ "./node_modules/bootstrap/dist/css/bootstrap.min.css", "./node_modules/ngx-bootstrap/datepicker/bs-datepicker.css", "src/styles.css" ], ...

Unique Text: "Personalized marker/pin for interactive map feature"

Looking to create a custom image map marker/pin with a unique bottom design resembling a union shape using CSS and Vue.js. I've attempted it myself but haven't been able to achieve the exact look as shown in the reference image. Any advice or ass ...

Change the image displayed when hovering over the div containing it

I've come to a roadblock while attempting to change an image when hovering over its containing div. I could achieve this using CSS, but I have 6 divs with 6 images inside them. If I were to use CSS, I'd need 6 sets of hover events to swap the ba ...

Tips for retrieving the output from an Azure Function

Just getting started with Azure Functions and I have this code snippet: module.exports = function (context, req) { context.log('JavaScript HTTP trigger function processed a request.'); context.log(context.req.body.videoId) ...

Combine javascript and css sequentially

I have a few tasks that involve combining CSS and JavaScript together. Here is an example of how I accomplish this: gulp.task('javascript', function() { return gulp.src([ libPath + 'jquery-2.1.4.min.js', libPath + '*.js& ...

jQuery disregards the HTTP request method and adds unusual query parameters to the URL

My current project involves utilizing a web service that requires POST requests containing a JSON string in the body. I've encountered some challenges while attempting to interact with this web service using jQuery: 1) jQuery appears to default to th ...

Strategies for adjusting text size to fit within a resizable div container

I'm facing an issue with a resizable div that has text inside. When I resize the div, the last line of the text goes beyond the boundaries of the div. How can I ensure that all the text fits within the resizable div while maintaining the appropriate f ...

Converting an object to JSON in javascript: A step-by-step guide

I have been attempting to convert my object person into a JSON format. const person = new Object(); person.firstName = 'testFirstName'; person.lastName = 'testLastName'; var myJson = JSON.stringify(person); ...

I'm just starting out with angular.js and could use some assistance in fixing the error below

I've encountered an error with my Angular code and would appreciate some assistance in resolving it. Please take a look at the code snippet below: <html ng-app> <head> <link rel="stylesheet" href="bootstrap.css"> <link rel="sty ...

When updating items in a FormView, the Dropdownlist may still hold the previous values

In a formview, there are two dropdownlists - one for cities and one for states. Whenever the state dropdownlist is changed, the city dropdownlist gets updated using javascript. If the city dropdownlist remains unchanged via javascript, the values of dlCi ...