vue js wait 1 second

To create a delay of 1 second (1000 milliseconds) in a Vue.js application, you can use JavaScript’s setTimeout function. Here’s how you can implement a 1-second delay in a Vue.js method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<template>
<div>
<button @click="waitOneSecond">Wait 1 Second</button>
<p v-if="showMessage">{{ message }}</p>
</div>
</template>

<script>
export default {
data() {
return {
showMessage: false,
message: "This message appeared after 1 second!",
};
},
methods: {
waitOneSecond() {
// Set showMessage to true immediately to show the message
this.showMessage = true;

// Use setTimeout to hide the message after 1 second
setTimeout(() => {
this.showMessage = false;
}, 1000); // 1000 milliseconds = 1 second
},
},
};
</script>

In this example, when the “Wait 1 Second” button is clicked, the waitOneSecond method sets showMessage to true, which displays the message. Then, it uses setTimeout to set showMessage back to false after a 1-second delay, which hides the message.

This creates a 1-second delay before hiding the message in your Vue.js application.