A step-by-step guide on automatically removing a Vuetify card within seconds

On my vue card, a success message is displayed and can be dismissed when the user clicks the ok button. I also want the card to disappear automatically after a few seconds, even if the button is not clicked. How can I achieve this? Below is my component code:

<template>
  <div v-if="show" class="notifications">
    <div class="globalSuccessWrapper">
      <v-layout>
        <v-flex xs12 sm6 offset-sm3>
          <v-card flat color="green">
            <v-card-title primary-title>
              <div>
                <h3 class="headline">New User Created</h3>
                <div>{{ card_text }}</div>
              </div>
            </v-card-title>
            <v-card-actions>
                <div class="close"><v-btn @click="removeMessage(0)">Ok</v-btn></div>
            </v-card-actions>
          </v-card>
        </v-flex>
      </v-layout>
    </div>
  </div>
</template>

<script>


export default {
  data() {
    return {
      card_text: 'Success!',
      show: true,
      notificationsToDisplay: [],
      graphQLNotifications: [],
    };
  },
  methods: {
    removeMessage(seconds, timeout) {
      if (!timeout) {
        setTimeout(() => {
          this.show = false;
        }, seconds * 1000);
      } else {
        timeout(seconds);
      }
    },
  },
};
</script>

<style scoped lang="scss">
  .globalSuccessWrapper {
    position: absolute;
    z-index: 10000;
    width: 100%;
  }
</style>

In the else case under timeout, I aim to implement this feature within the removeMessage function.

Answer №1

It's not possible to trigger this action directly from the "removeMessage" function, as you need it to occur automatically without user intervention.

The recommended approach is to set up a watcher for changes on the "data.show" property. You can find detailed instructions on how to implement this here.

Within the watcher, you can use a timeout mechanism to handle the notification closure when the value of "data.show" becomes true. Save the identifier returned by setTimeout function and set "data.save" to false after the designated timeout period. Remember to clear the timeout either in the "removeMessage" function or inside the watcher if the value turns false.

Clearing the timeout upon closing the notification is crucial to prevent unexpected behavior such as:

  • An open notification
  • User clicks close button
  • Another notification opens while the previous one is about to close
  • The premature closure of the second notification due to timeout firing

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

Javascript syntax error: Unexpected ending of data while trying to parse JSON data at line 1, column 1

I operate a CS:GO betting platform, and I encountered an issue when attempting to access the page for withdrawing skins. After completing the reCAPTCHA verification process to confirm that I am not a robot, I received the following error: Javascript err ...

Having trouble retrieving a value from the $http promise, causing the code within the then() function to not run as expected

In the past, I encountered a challenge with the $http service which I managed to solve by creating a dedicated service for handling my requests. However, as my requests grew larger, this solution started to seem inefficient. Instead of just assigning a sim ...

The function cannot be called on params.getValue()

I am facing an issue while trying to retrieve the value of a specific row's column ID. The goal is to change the URL by appending the retrieved ID when clicking on an icon. However, I encountered an error stating TypeError: params.getValue is not a fu ...

What is the process for selecting and clicking a specific div with a distinct class name in HTML

Hey there! I'm a beginner at coding with puppeteer and I have a question. How can I make my code click on this image: (image) The current code I have looks like this: const puppeteer = require('puppeteer'); (async () => { const bro ...

Dynamically fill out form inputs in a React application

I have 3 materialUI TextFields which are dynamically rendered n number of times (n is an integer input from the user before rendering the form field, stored in a variable called groupMembersCount). The dynamic rendering is implemented as follows: export d ...

Ways to showcase a website within an HTML document using a URL?

Is it possible to show 2 webpages on a single aspx webpage? For instance, When a user opens the link for www.mywebsite.com, I would like to display both www.google.com and www.bing.com on my homepage. Behind the scenes, I will call two separate URLs an ...

Tips for effectively displaying camera.position.set (x,y,z) and camera.lookAt (x,y,z) within a three.js environment

While going through the tutorial on drawing lines in three.js documentation, I came across this code snippet: The code seems to be perfectly fine without any issues. <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...

Using a prop array as v-model in a Vue JS CheckBoxGroup implementation

Struggling to create a reusable CheckBoxGroup component with a prop array as v-model. I checked out the vuejs guide at https://v2.vuejs.org/v2/guide/forms.html#Checkbox which uses the v-model array in the data of the same component. However, this approach ...

Component failing to refresh with each key modification

My understanding is that adding a key attribute to a component should make it reactive when the key changes. However, with a v-navigation-drawer from Vuetify, this doesn't seem to have any impact. I've tried making arbitrary changes to the logge ...

Ensure that an object is within the frustum by utilizing THREE.js

I've been experimenting with Three.js and have a canvas that I want to use as a GUI. To achieve this, I need to check if an object is within the camera's frustum. Here is my current code snippet: camera.updateMatrix(); camera.updateMatrixWorld() ...

Tips for rearranging an item to the beginning of an array at index 0

What is the most efficient way to delete a specific object from an array and move it to the beginning? I have attempted using regular methods such as finding the index, manually splicing, and then moving the object to the top. farmer: [ { id:1, name: "na ...

Utilize a pipe to seamlessly transfer data from MSSQL to a Node.js application

I am currently utilizing node along with node-mssql version 6.0.1 for handling large amounts of data retrieval from the database and transmitting it to the frontend using streams. Although I have attempted to implement pipe and stream functionality as rec ...

Animating content to slide into view in the same direction using CSS transitions

My goal is to smoothly slide one of two 'pages', represented as <divs>, into view with a gentle transition that allows the user to see one page sliding out while the other slides in. Eventually, this transition will be triggered from the ba ...

Angular animations: triggered by user interaction

I'm encountering some difficulties with animating elements in my Angular application. The issue involves a grid made up of cells (created using ng-repeat). What I'm aiming to achieve is a simple animation: when a cell is clicked, it should disapp ...

"Exploring the Power of Logarithmic Slider with Vue and Quasar

I'm currently working on a project utilizing Vue 2 and Quasar 1, where I am attempting to develop a logarithmic slider. Initially, I managed to create a basic example using a native input slider in this code pen: https://codepen.io/tonycarpenter/pen/Z ...

Searching and Sorting through JSON Data in React Native

I am currently expanding my knowledge in React Native and experimenting with filtering a JSON data set on a screen. My goal is to display only the filtered results on the screen. Would it be advisable for me to create a new component called FilteredTicket? ...

Executing a serverless function in Next.js using getStaticPaths: A step-by-step guide

In my current project, I am utilizing Next.js and the Vercel deployment workflow. To set up page generation at build time, I have been following a guide that demonstrates how to generate pages based on an external API's response. // At build time, t ...

AJAX allows developers to declare variables within their code to

Here is a snippet of my JavaScript code: $(\"img.agree\").click(function() { var follow_id = $(this).attr(\"id\"); var user_id = $(\"img.user_id\").attr(\"id\"); $.ajax({ type: \"POST& ...

Loading intersecting objects with FBXLoader in Three.js

After successfully loading an fbx file using FBXLoader and adding it to the scene object, I encountered an issue where I couldn't interact with the object on click to apply transform controls. Interestingly, all other objects were clickable except for ...

Tips for accessing the selected button in notification.confirm() within a PhoneGap app

Recently, I implemented the Phonegap notification.confirm in this way: navigator.notification.confirm( 'Do you wish to proceed?', function() { console.log("Function Called"); }, 'Game Over', 'Continu ...