Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
50
rated 0 times [  57] [ 7]  / answers: 1 / hits: 15624  / 5 Years ago, wed, february 13, 2019, 12:00:00

I have a button, user can click the button more than one time if he want. But when the user click the button, he might accidentally click twice, in such cases second click should be blocked by the code.


If I further explain. They should be a small delay between two clicks.


How do I achieve this using vue js?


In Vue docs Event modifiers
I found .stop


<button @click.stop="myFunction">Increase</button>

Does this do the job what I want?


More From » vue.js

 Answers
44

No, the .stop modifier does not solve your problem. What that modifier does is to prevent event propagation (it is equivalent to stopPropagation() in plan JavaScript)



You could use the .once modifier to prevent any further events after the first one. However, if you want to allow multiple clicks, but have a delay between them, you could do something like this:



<template>
<button :disabled=disabled @click=delay>Increase</button>
</template>

<script>
export default {
data () {
return {
disabled: false,
timeout: null
}
},
methods: {
delay () {
this.disabled = true

// Re-enable after 5 seconds
this.timeout = setTimeout(() => {
this.disabled = false
}, 5000)

this.myFunction()
},
myFunction () {
// Your function
}
},
beforeDestroy () {
// clear the timeout before the component is destroyed
clearTimeout(this.timeout)
}
}
</script>

[#52607] Friday, February 8, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
harleyterryp

Total Points: 290
Total Questions: 92
Total Answers: 95

Location: Montenegro
Member since Sun, May 7, 2023
1 Year ago
;