Countdown function needs to be implemented in many projects, for example: sending verification code. Now let's take an example to implement a simple countdown button function. Simple layout, simple operation, simple effect, and most importantly, understanding of the ideas and countdown steps! ! ! For example, the code is as follows: Requirements: Click the submit button and count down for five seconds. During the countdown, the input box and submit button are disabled. After the countdown ends, the input box and submit button return to normal state. 1. First implement the required HTML layout and add click events <div> <!-- disabled is true to disable --> Input box: <input type="text" :disabled="istrue"> <button @click="addHandle" :disabled="istrue">Submit</button> <!-- Countdown text prompt--> <p>{{this.txt}}</p> </div> 2. Click the submit button and the countdown starts to change to disabled state. Define a timer <script> export default { data(){ return { txt:'', istrue:false, inp:'' } }, methods:{ addHandle(){ //define n=5 seconds let n=5 //Define the timer time let time = setInterval(()=>{ //Disable this.istrue=true //Change the countdown text prompt this.txt=n+'Submit in seconds' n-- //If n<0, clear the timer, cancel the disabled state, and the text prompt is empty (not displayed) if(n<0){ this.txt="" this.istrue=false clearInterval(time) } },1000) } } } </script> The ideas and steps are written in the comments above, and a simple countdown is easily achieved. Overall code: <template> <div> <!-- disabled is true to disable --> Input box: <input type="text" :disabled="istrue"> <button @click="addHandle" :disabled="istrue">Submit</button> <!-- Countdown text prompt--> <p>{{this.txt}}</p> </div> </template> <script> export default { data(){ return { txt:'', istrue:false, inp:'' } }, methods:{ addHandle(){ //define n=5 seconds let n=5 //Define the timer time let time = setInterval(()=>{ //Disable this.istrue=true //Change the countdown text prompt this.txt=n+'Submit in seconds' n-- //If n<0, clear the timer, cancel the disabled state, and the text prompt is empty (not displayed) if(n<0){ this.txt="" this.istrue=false clearInterval(time) } },1000) } } } </script> The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
>>: Docker container monitoring and log management implementation process analysis
Vue+Openlayer uses modify to modify elements. The...
Table of contents Using routing plugins in a modu...
Here is a case that front-end developers must kno...
1. What are CSS methodologies? CSS methodologies ...
Table of contents Overview The role of reverse pr...
Preface InnoDB stores data in tablespaces. In the...
In many projects, it is necessary to implement th...
Table of contents Component Design Defining the f...
Practice is the only way to test the truth. This ...
Sometimes the page is very long and needs an arro...
1. The role of doctype, the difference between st...
Table of contents 1. Effect diagram (multiple col...
Due to the advantages of GTID, we need to change ...
When processing batch updates of certain data, if...
EXPLAIN shows how MySQL uses indexes to process s...