Managing input debounce durations in VueJS with dynamic wait times

Managing debounce functionality for input fields in Vue is usually straightforward. However, I am facing a challenge when it comes to making the debounce wait time configurable on a per-field basis.

Each of my input fields is associated with an object structured like this:

filters: [
    {
    title: 'Foo',
    value: null,
    debounce: 1200
  },
    {
    title: 'Bar',
    value: null,
    debounce: 400
  }
]

These objects are used to create the input fields as follows:

<div v-for="filter in filters">
    <input type="text" @input="debounceInput($event, filter)" :value="filter.value">
    <span>{{ filter.value }}</span>
</div>

In order to implement debounce functionality, I am utilizing the Lodash debounce method within the following method:

methods: {
    debounceInput: _.debounce((event, filter) => {
        filter.value = event.target.value;
    }, 500)
}

However, my goal is to make the debounce wait time configurable based on the value stored in the filter object. Despite attempting various approaches, such as wrapping the _.debounce call in an anonymous function or using bind and call, I have been unable to access the debounce time configured at the field level.

If you would like to explore the code further, you can do so through the JSFiddle link provided: https://jsfiddle.net/thebluenile/ma6nvqzh/

Is there a solution to this configuration challenge within the debounce functionality?

Answer №1

Invent a technique for producing the debounce functions for every single filter:

generateDebouncers() {
  this.filters.forEach(filter => {
    Vue.set(filter, 'debouncer', _.debounce(function(event) {
      filter.value = event.target.value;
    }, filter.debounce));
  });
}

This method establishes a debouncer attribute on each individual filter, serving as a function to be invoked within the template in this manner:

<input type="text" @input="filter.debouncer" :value="filter.value">

You can initiate this in the created section or trigger it within a profound watch on filters if you require it to respond to filter duration modifications.

Below is a demonstration

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

Parse a string containing a selection of markdown by using regular expressions for tokenization

I need to tokenize a string using a regular expression that consists of markdown formatting. Specifically, bold text is denoted by **text** and italic text is denoted by _text_. To tokenize the string "a _b_ c **d** _e", it should be split into ['a & ...

The combination of Laravel 5.5 and Vue.js 2.x is experiencing issues when trying to run 'npm run dev'

After setting up a new project with Laravel 5.5 and Vue.js 2.x, I encountered a problem when running the following commands: npm install npm run dev > @ dev /Volumes/Work/Work/Vue/CRUD > npm run development > @ development /Volumes/Work/Work ...

In Vue.js, I only want to retrieve and display the parent's ID or name once for each of its child components

<td v-if="currentId != loop.id" class="text-center"> <div :set="currentId = loop.id">{{ loop.id }}</div> </td> <td v-else></td> Looking to achieve a specific layout like this This invo ...

updating container with wire:ignore in laravel livewire

Incorporating a Vue component into a Livewire component, specifically using a DatePicker component: <div id="customDiv" class="col-12 col-md-6 m-1" wire:ignore> <date-picker v-model="date" ...

Combining Multiple Arrays into a Multidimensional Array

I'm struggling to find information on how to combine multiple arrays of the same length into a multidimensional array. For example, I have three arrays: array1 = [value1a1, value2a1, value3a1]; array2 = [value1a2, value2a2, value3a2]; array3 = [value ...

Creating a scrollable div with a fixed background using HTML

Attempting to create a full-screen image background with a scrollable div in its center, I crafted the following code: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Bio</title> <link href="../css/layout ...

Setting a Value?

Within the services.js/Cordova file, I am encountering an issue with the following code: .factory('GCs', ['$http', function($http) { var obj= {}; $http.post("mydomina.com?myrequest=getbyid", { "id": "1"} ) ...

Can Selenium successfully scrape data from this website?

I am currently attempting to extract Hate Symbol data (including the name, symbol type, description, ideology, location, and images) from the GPAHE website using Selenium. As one of my initial steps, I am trying to set the input_element to the XPATH of the ...

Preventing unauthorized access via PHP

First and foremost, I want to clarify that I don't have much experience in PHP or database programming. That being said, I am proceeding with caution in my current project. Essentially, I am working on a website that is database-driven, and I have im ...

Is there a way to position two Grid elements to the left and one to the right in Material UI, especially if the first Grid element has a specified width

I'm currently using Material UI and have a Grid layout with three items. The left item needs to have a fixed width of 240px and be aligned to the left. The middle item should be left justified and can have any width, containing buttons that I've ...

Is it necessary for me to set up @types/node? It appears that VSCode comes with it pre-installed

Many individuals have been adding @types/node to their development dependencies. Yet, if you were to open a blank folder in VSCode and create an empty JavaScript file, then input: const fs = require('fs'); // <= hover it and the type display ...

What is the best way to connect users for chatting?

I am currently developing a cutting-edge chat application with a real-time communication server that allows me to send and receive messages seamlessly. The next step in my project involves mapping users for private real-time communication. I'm faced w ...

Exploring the possibilities of jQuery with Accordion functionality and creating dynamic multiple menus

Incorporating the Wayfinder and Accordion menus, I have set up a two-level menu structure for the left column. The structure looks like this: <ul class="accordion">: Menu 1 Sub-menu 1.1 Sub-menu 1.2 Sub-menu 1.3 Menu 2 Sub-menu 2 ...

Disable the function when the mouse is moved off or released

My custom scrolling implementation in a ticker using Jquery is due to the fact that standard scrolling doesn't function well with existing CSS animations. The goal is to enable movement of the ticker when clicking and dragging on the controller, a div ...

displaying specific items in a vue list based on status conditions

Here is my Vue code snippet (and a working fiddle): https://jsfiddle.net/he26tn53/: <div id="app"></div> new Vue({ el: "#app", template:`<h2>People:</h2><span @click="showOnlyEnabled = !showOnlyEnable"> toggle enabled< ...

Discovering a specific value within a JSON stringified array

I am looking for a specific value within a JSON stringify array [{"id":"432","temperature":"1","humidity":"1","createat":"0000-00-00 00:00:00"},{"id":"433","temperature":"22.00","humidity":"48","createat":"2015-10-11 19:49:57"},{"id":"434","temperature":" ...

How to handle a Node.js promise that times out if execution is not finished within a specified timeframe

return await new Promise(function (resolve, reject) { //some work goes here resolve(true) }); Using Delayed Timeout return await new Promise(function (resolve, reject) { //some work goes here setTimeout(function() { resolve(true); }, 5000); } ...

Discovering methods to access properties from another function

Can I access the value of variable w from another function? <script type="text/javascript"> var first = { myFirst: function(){ var w= 90; var q=12; }} var second= { mySecond: function(){ first.myFirst.w }} </script> ...

It is impossible to resolve Npm vulnerabilities

My journey with learning React began when I decided to create my first app using the command: 'npx create-react-app my-app' However, upon building the app, I encountered a warning in the terminal: 22 vulnerabilities (9 moderate, 13 high) In ...

Tips for generating Sequelize models dynamically in NodeJS 14 using Sequelize version 6

My project relies on Node and Sequelize for its backend operations. Everything was smooth sailing until I decided to update both Node and Sequelize to their latest versions. Although I could revert back to the older versions, I'm hesitant because my o ...