Steps for accessing the event object within a debounced method in VueJS

Having difficulty accessing the event object in a debounced method:

methods: {
  fetchData: _.debounce(function(e) {
    console.log(e) // returns undefined
  }, 500)
}

Is it feasible to retrieve the event object in a method? I need to determine which keycodes were pressed:

if (e.keyCode >= 65 && e.keyCode <= 80) {
  // perform some actions
}

The fetchData method is triggered as follows:

<input @keyup="fetchData()" v-model="name" type="text">

Answer №1

Check out this example using vue js, make sure to include @keyup="fetchData($event)" which passes the event into debounce function

new Vue({
  el: '#app',
  data() {
    return {
      keywords: ''
    }
  },
  methods: {
    fetchData: _.debounce(function(e) {
      console.log(e.keyCode) // result will be undefined
    }, 500)
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.min.js"></script>
<div id="app">
  <input id="textInput" @keyup="fetchData($event)" />
</div>

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

Uploading files with jQuery using Rails and CarrierWave with Amazon S3 storage

I'm relatively new to using jquery file uploading plugins and libraries. Currently, I am working on developing an image uploader that can load images via jquery/ajax on the frontend without requiring a site update as the image is uploaded. Then, I ne ...

Error message when trying to get tree from Json using jqTree: "Oops! $(...).tree is not a valid function."

Currently, I am utilizing jqTree to display JSON data in a tree format. However, as I was implementing the demo of jqTree, an error occurred: "Uncaught TypeError: $(...).tree is not a function" ...

The Formik form is not being populated with data from the API

While working on a functional component in ReactJS, I created a simple form using Formik. The input fields in my form are supposed to fetch data from an API when the component mounts. To achieve this, I utilized fetch and useEffect. However, after fetching ...

Utilizing JavaScript for loops to extract the final element from an array

I am facing an issue with the second loop within a function that goes through a JSON file. The problem is that it only returns the last item in the array. I need to figure out how to fix this because the chart object should be created on each iteration, ...

The 'BaseResponse<IavailableParameters[]>' type does not contain the properties 'length', 'pop', etc, which are expected to be present in the 'IavailableParameters[]' type

After making a get call to my API and receiving a list of objects, I save that data to a property in my DataService for use across components. Here is the code snippet from my component that calls the service: getAvailableParameters() { this.verifi ...

Troubleshooting Firefox problem with HTML frame communication and JavaScript

I have encountered a peculiar issue that seems to only occur in Firefox (version 3.6.6 and older versions of 3.6). Allow me to walk you through the scenario: There are two HTML pages: Page-A & Page-B. Page-A includes an iframe element, with its sour ...

Adjusting the height of a div in real-time

Similar Query: Height of a div Greetings, I'm looking to create a DIV element with its height based on the visible area of the browser window minus 100px. Is there a solution that would automatically adjust the height of the DIV when the user re ...

What is the best way to extract individual words from a string based on a specified list of tokens?

I am currently working with a list of tokens used to create artificial Japanese words, which is represented by the following array: var syllables = ["chi","tsu","shi","ka","ki","ku","ke","ko","ta","te","to","sa","su","se","so","na","ni","nu","ne","no","ha ...

Error: Unable to access the 'clearAsyncValidators' property as it is undefined when running Jest tests on an Angular component

Encountering an Error While Testing Components with Jest Here is my code block for onClickLogin() method: onClickLogin() { if(this.loginForm.valid) { this.api.getLoginData().subscribe(res => { const user = res.find(a => { ...

The code is functioning properly, however it is returning the following error: Anticipated either

Can you explain why this code is resulting in an unused expression error? <input style={{margin:'25px 50px 0',textAlign:'center'}} type='text' placeholder='add ToDo' onKeyPress={e =>{(e.key ==='En ...

How to implement autoincrement feature using Mongoose Sequence in NodeJS

When attempting to set up automatic incrementation for a field using Mongoose Sequence, I encountered an issue where the field was not auto-incrementing and instead resulted in an error message from Mongoose. The error stated: Error: Product validation f ...

Execute PHP script through jQuery request within the context of a Wordpress environment

I want to replicate a specific functionality in WordPress. In this functionality, jQuery calls a PHP file that queries a MySQL table and returns the result encapsulated within an HTML tag. How can I achieve this? <html> <head> <script ...

The Vue-router is failing to display the appropriate component

I'm facing a recurring issue with vue-router where it constantly redirects me back to the initial component I have routed to / Here is how the router is initialized: export default new Router({ history: true, routes: [ { path: '/& ...

Loading Ajax dropdown with a preselected value in the dropdown menu

There are two pages, each containing a form. Page 1 - Form A) Includes a dropdown menu (with values populated from the database) and a continue button. <select name="form[formA][]" id="dropdown1"> <option value="1">Option 01</opti ...

The boolean value remains constant when using useState

An Alert module pops up when incorrect credentials are entered by the user. It includes a close button designed to hide the alert. The alert function operates correctly on the first instance, displaying a boolean value of true upon activation and switching ...

Utilizing React with Material UI, implement a Select component while disabling the scroll lock and ensuring the menu is positioned relative to

import React from "react"; import "./styles.css"; import Input from "@material-ui/core/Input"; import MenuItem from "@material-ui/core/MenuItem"; import FormControl from "@material-ui/core/FormControl"; import Select from "@material-ui/core/Select"; cons ...

While holding down and moving one div, have another div seamlessly glide alongside it

I have a scenario where two divs are located in separate containers. My goal is to drag div2 in container2 while moving div1 in container1. The 'left' property for both divs should remain the same at all times. I can currently drag each div indi ...

halt execution of npm test and erase any printed content

When I run npm test on my React project, it runs unit tests using jest and react testing library. The test logs (including console log lines added for debugging) are printed to the screen but get deleted after running the tests. It seems like the logs are ...

The ng-click event for the reset button is limited to a single use

There seems to be a problem with the reset button functionality on my webpage. Although it initially works, it only works once and then requires a reload of the page to function again. Here is the JS code: var ctrl = this; var original_device = angular.c ...

Retrieve image data in its original format using AJAX

Currently, I am working on integrating Facebook with a website and I encountered a specific call in the Facebook API that posts a picture to a user's account. One of the parameters required is the raw image data. The image is stored locally on the web ...