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:
<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.