Unable to delete touchmove event - Vue watching system

Preventing scrolling on mobile devices:

const stopScroll = function(e) {
  e.preventDefault()
}

Adding the listener:

document.body.addEventListener('touchmove', stopScroll, { passive: false })

Removing the listener:

document.body.removeEventListener('touchmove', stopScroll)

When testing in Chrome DevTools, it is observed that adding the listener successfully prevents touch scrolling. However, when attempting to remove the event listener using removeEventListener, it does not seem to work as expected even though chrome dev tools indicate the event has been removed. This functionality is being implemented within a Vue watcher:

watch: {
  chatWindow(newValue) {
    if (newValue) {
      // add listener
    } else {
      // remove listener
    }
  }
}

Answer №1

Make sure to relocate the hideScroll function from your watcher.

Currently, you are generating a new function every time the watcher is triggered. Even though the function appears identical each time, it's not the exact instance linked to the event listener. This mismatch is causing you difficulty in removing it.

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

Selecting random numbers in React.js

I am working on creating a Netflix Clone and I want to change the banner image every time the page refreshes. I have integrated movie details from TMDb and have an array containing these details. My goal is to select a random number between 0 and 19 and us ...

Unpredicted organization when combining react, mapbox-gl, custom layer, and three.js

I am currently working on utilizing mapbox gl to showcase gltf buildings through a custom layer. Additionally, I am displaying the default 3d-buildings provided by mapbox in this manner: export default { id: '3d-buildings', source: 'c ...

Utilize Axios in Vue.js to fetch and process data from a REST API endpoint in C#

After successfully creating and deploying an API on Azure, I am trying to display the response in an alert box using javascript (Vue.js). The test method of my API returns the string "working". You can test the API here. This is the code snippet from my A ...

Exploring jQuery: Choosing all li elements starting from a specific index to the end

Is there a way to select the last item, li:eq('+(lastItemIndex)+')', and all subsequent items until the end? Check out this JSFiddle demo for an example of changing elements in li from 3 onwards. ...

Is it possible to utilize Angular.js as a full substitute for a traditional JavaScript template engine?

While angular excels in two-way data binding and single-page applications, could it also serve as a viable replacement for a JavaScript template engine? Let's dive into the potential pros and cons of this approach. ...

Troubleshooting JavaScript Date Problem with Internet Explorer 7

When I retrieve a Date from a web method, it comes in the format of "Mon Sep 30 07:26:14 EDT 2013". However, when I try to format this date in my JavaScript code like this: var d= SomeDate.format("MM/dd/yyyy hh:mm:ss tt"); //Somedate is coming from the we ...

Exploring values within an array of objects using Node.js

I have some data presented in the following format [ [ '@test','1.2.6' ], [ '@test2','4.0.1' ], [ '@test3','2.2.0-unstable' ], ... ] My goal is to extract all values that include -unstable, ...

Error message received while converting webm video to images using Kagami/ffmpeg.js: Unable to locate an appropriate output format for '%04d.jpg'

These are the current versions: node v12.9.1, npm 6.10.2, [email protected] Repository: https://github.com/Kagami/ffmpeg.js The code in decode.js looks like this: const fs = require('fs'); const ffmpeg = require('ffmpeg.js'); c ...

Guide on extracting unique key values from an array by utilizing a different key

I have an array containing the names of products along with their storage capacities. let products = [{name: "samsung A", storage: "128GB"}, {name: "samsung B", storage: "128GB"}, {name: "samsung C", storag ...

:Incorporating active hyperlinks through javascript

Hey there, I've encountered a little conundrum. I have a header.php file that contains all the header information - navigation and logo. It's super convenient because I can include this file on all my pages where needed, making editing a breeze. ...

Angular controller fails to fetch scope variable in subsequent view

I'm currently working on implementing a login feature that directs a validated user to a personalized HTML view that greets them with their name. The initial view is a login page. When the user clicks the "Log In" button, the ng-click function is sup ...

Using JQuery to pass a concatenated variable

What is the reason behind the success of this code: $(".ab").css({'background':'#ce0000','color':'#EEE'}); While this code does not work: f("ab"); function f(ab){ var x = '".'+ ab +'"'; ...

It seems like my ajax function is functioning properly, but the data is not getting submitted

Having trouble sending the value of my selector to a PHP file through AJAX. The success function is working, and when I directly visit "your_php_script.php" it functions as expected. However, why is the PHP page not showing up on the page with the AJAX r ...

The route parameters in Vue SSR are being retrieved from a separate request

I am currently using Akryum/vue-cli-plugin-ssr along with vue-cli, but I have encountered an unusual issue in my Vue SSR application. It seems that the route params are getting mixed up with requests made either before or in parallel to the current one. F ...

Altering the value of a form upon submission

Struggling with Javascript to update an input value before submitting and storing it in a MySQL table, I encounter the issue of getting a blank row in the database. The code snippet causing this problem looks like this: document.getElementsByName('w ...

Need help fixing my issue with the try-catch functionality in my calculator program

Can someone assist me with my Vue.js calculator issue? I am facing a problem where the output gets added to the expression after using try and catch. How can I ensure that both ERR and the output are displayed in the {{output}} section? Any help would be a ...

What is the sequence in which Jest executes its tests?

A fascinating challenge I've taken on involves testing a card game created in JavaScript using Jest. Currently, I have developed two tests: one to verify the creation of a 52-card deck and another to confirm that the player is dealt two cards at the ...

Determine if the browser displays <select multiple> as a modal dialog

Is it possible to use JavaScript to detect whether a specific browser displays a focused <select multiple> element as a popup or strictly as an inline box? On certain platforms, like the Android Browser and iOS Safari, the appearance of a popup can ...

Experiencing trouble with detecting intersections using the Three.js raycaster

After successfully writing a code to intersect some objects, I encountered an issue when adding a canvas along with other div elements in the HTML document. Now, there seems to be no intersection on the object. You can observe this live example here. If yo ...

Facing compatibility problems with JavaScript and Cascading Style Sheets in Google Chrome?

Welcome to the site . Let's address a couple of issues with JavaScript and CSS: Firstly, the JS code seems to be malfunctioning only in Chrome, throwing an error that reads: 'Uncaught TypeError: Object [object Object] has no method 'on prij ...