Avoiding future clicks with a delay in VUE: A guide

Is there a way to prevent the next click for 600ms after an initial click? I want to temporarily "disable" all a tags for 600ms after one is clicked. Any help would be appreciated.

VUE

  
  <ul>
    <li v-for="item in navigation" :key="item" :title="item.name">
      <router-link :to="item.path" @click="delay()">{{ item.name }}</router-link>
    </li>
  </ul>

delay()

  
  methods: {
    delay() {
      this.event.preventDefault();
    }
  }

EDIT I have made some changes. Now, I want each button to be disabled for 600ms after being clicked before becoming active again. The router should not be included in this timer. I tried something like this but the buttons remain permanently disabled


<ul>
  <li v-for="item in navigation" :key="item" :title="item.name">
    <button type="button" disabled="false" @click="delay(item.path)">{{ item.name }}</button>
  </li>
</ul>

method


methods: {
  delay(to) {
    this.$router.push(to);
    setTimeout(() => this.disabled = true, 600)
  },
},

Answer №1

If you want to control the link enable/disable behavior, consider using setTimeout in the following way:

<a :disabled="!linkEnabled" href="javascript:void(0)" @click="delay(item.path)">{{ item.name }}</a>

Here is how you can implement it in your function:

delay(to) {
    this.linkEnable = false;
    this.$router.push(to);
    setTimeout(() => this.linkEnable = true, 600)
}

Answer №2

If you want to create a router, you can set up routes and use the beforeEach method to listen for router changes and pause for a specified amount of time.

const routes = [{
    path: "/login",
    name: "Login Component",
    component: LoginComponent
}]
const router = new createRouter({
    history: createWebHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    // Add in your waiting logic here before moving to the next route
}

export default router;

For more information on Vue Router, check out Vue Router documentation

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

What are the steps to connect to multiple databases with ExpressJS?

I have a server with 3 databases containing identical tables. The databases are named DB1, DB2, and DB3. When working with a specific database, I utilize the following code in app.js: var cnxDB = require('./routes/cnxDB'); app.post('/user ...

Move the modelValue logic to a composable function

I'm currently transitioning from Vue 2 to Vue 3 and facing some challenges with composables. I have a group of components that inherit the modelValue property. Consequently, I find myself duplicating this code for each component utilizing modelValue ( ...

What is the most efficient way to align a localStorage variable with its corresponding page?

In my current project, I have developed a component that is utilized in an online science lab setting. To ensure continuity for researchers who navigate away from the page and return later, I have integrated the use of localStorage. The goal is to preserv ...

Guide me on setting up a progress bar using the XHR function to display the upload percentage

let formData2 = new FormData(); formData2.append('_token', vm.response._token); formData2.append('file', vm.response.content[i].path); formData2.append('type', vm.res ...

Storing the result of parsing JSON data into a global variable

$(function() { var countFromData = 0; getReminder(); alert(countFromData); }); function getReminder() { $.getJSON("<?=base_url()?>home/leavereminder", {}, function(data) { ...

Create a new array by dynamically generating a key while comparing two existing arrays

One of the features in my app involves retrieving data from an API and storing it in $scope.newz. The previous user activity is loaded from LocalStorage as bookmarkData. I am facing a challenge with comparing the contentId values in arrays $scope.newz an ...

What is a memory-saving method to clear an object in JavaScript?

I am looking for a way to use the same object repeatedly in JavaScript by emptying it after its purpose is served, without creating a new object each time. In arrays, I usually do arr.length=0 to clear an array instead of assigning it to a new memory locat ...

Unable to retrieve content using the query.ID in Next.js

I'm trying to figure out what is causing the issue in this code, but I can't seem to resolve it. My goal is to use the query.id inside getInitialProps to fetch some content. The fetching process works fine, but when an error occurs, I receive thi ...

What is the purpose behind enclosing a function expression in parentheses in JavaScript (apart from creating an IIFE)?

While delving into some advanced JavaScript concepts, I stumbled upon a piece of jQuery code on GitHub that caught my eye. It was an anonymous function wrapped in parentheses, which you can find here. Although I'm familiar with Immediately Invoked Fu ...

Web scraping with Cheerio in Node.js sometimes yields undefined results

When attempting to extract data from NSE website, I initially tried using the inspect element console: (Edited the question) objs = $('div[class="table-wrap"] > table > tbody > tr > td').slice(0, 8) objs.map((i,element) =& ...

The success method in the observable is failing to trigger

Could someone explain why the () success method is not triggering? It seems to be working fine when using forkjoin(). Shouldn't the success method fire every time, similar to a final method in a try-catch block? Note: Inline comments should also be c ...

How do useCases interact with each other within Clean Architecture principles in NodeJS?

I'm currently working on implementing Bob Martin's Clean Architecture in my project, and I have a question. How do use-cases interact with each other? For instance: In my project, there are entities for Department and Employee. The Department ...

"Accessing your account only requires a simple two-click login process

Why do I need to click the log in button twice for data validation on my forum website? While designing a forum website, I noticed that users have to click the log-in button twice before the system validates and processes the input data. Below is the code ...

The console is showing the Ajax Get request being logged, but for some reason it is not displaying on the

Could someone please explain why this response isn't displaying on the page? $.ajaxPrefilter( function (options) { if (options.crossDomain && jQuery.support.cors) { var http = (window.location.protocol === 'http:' ? &apos ...

Switch out the arrow icon in the dropdown menu with an SVG graphic

Looking for a way to customize the dropdown caret in a semantic-ui-react component? Here's how it currently appears: <Dropdown className="hello-dropdown" placeholder="Comapany" onChange={this.someFunction} options={som ...

What is the purpose of the "Dot" symbol in the "runtimeArgs" property of the launch.json file in Visual Studio Code?

As I opened my Visual Studio Code today, a notification greeted me about a new update. Without hesitation, I went ahead and installed it, assuming it would be like any other update I've had in the past. However, after updating to version 1.22.1, I enc ...

Stop user from navigating to specific route using React-router

I am currently utilizing react-router with history useQueries(createHashHistory)(), and I have a requirement to restrict navigation to certain routes based on the route's configuration. The route configuration looks like this: <Route path="/" name ...

Express.js continues to retrieve outdated query results that have already been removed from the database

I am experiencing a strange issue with my PostgreSQL and Express.js setup. Despite updating my database with new entries and deleting old ones, it seems to only display the old data that was deleted days ago. It's almost as if it's stuck in some ...

Maintaining a reference to an element while binding event handlers in a prototype

Is there a way to utilize a bound event handler in prototype while maintaining the "this" context? It seems that doing so disrupts the default binding provided by prototype for event handlers. According to the documentation: The handler's context ...

The issue of overflow-y not functioning properly in conjunction with ng-repeat has been observed

As indicated in this resource, setting a fixed height for the div is suggested, but it seems like it's not working with the code provided below. <div style="margin-top:0.5vh;"> <div style="height:200px;border:1px solid red;overflow:au ...