JavaScript to implement the countdown for sending SMS

JavaScript to implement the countdown for sending SMS

This article shares the specific code of JavaScript to implement the countdown of sending SMS for your reference. The specific content is as follows

Implementation ideas:

1. js gets the send button element object
2. Set a sending interval (global variable)
3. Bind the click event to the send button element object - - -onclick,
Click event handler:
① After clicking, the button is set to disabled—disabled: true;
② Use the timing function with a time interval of 1 second.
Function handler for timed function calls:
Determine whether the time is 0
Not 0 - - - The description of the button changes to: How many seconds are left, and the time is reduced by 1
If it is 0, the button can be clicked and the description inside is restored to "Send"

Code example:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Send SMS</title>
    <style>
        .box {
            width: 360px;
            margin: 100px auto;
        }
    </style>
</head>

<body>
    <div class="box">
        Mobile phone number: <input type="number">
        <button>Send SMS</button>
    </div>

    <script>
        var btn = document.querySelector('button');
        var time = 10;
        btn.addEventListener('click', function() {
            btn.disabled = true;
            var timer = setInterval(function() {
                if (time == 0) {
                    // Clear the timer and restore the button clearInterval(timer);
                    btn.disabled = false;
                    btn.innerHTML = 'Send SMS';
                    time = 10;
                } else {
                    btn.innerHTML = 'remaining' + time + 'seconds';
                    time--;
                }
            }, 1000);
        });
    </script>
</body>

</html>

Page effect:

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:
  • JavaScript implements 60s SMS countdown
  • JS implements the button countdown function after sending SMS verification (to prevent refresh countdown from failing)
  • JS gets the SMS verification code countdown implementation code
  • js implements SMS sending countdown function (regular verification)
  • js implements 5-second countdown to resend SMS function
  • JS implements the function of obtaining SMS verification code and countdown when users register
  • Implementation of JS SMS verification code countdown function (no verification code, only countdown)
  • A simple way to implement a countdown to send SMS in js
  • Implement the countdown function after sending the SMS verification code based on JS (ignore page refresh, and do not perform the countdown function when the page is closed)
  • Implementing SMS button countdown based on JavaScript (super simple)

<<:  Solve the problem of multiple listeners reported when starting tomcat in Idea

>>:  How to migrate local mysql to server database

Recommend

The difference between redundant and duplicate indexes in MySQL

MySQL allows you to create multiple indexes on a ...

Example code for using HTML ul and li tags to display images

Copy the following code to the code area of ​​Drea...

How to install WSL2 Ubuntu20.04 on Windows 10 and set up the docker environment

Enable WSL Make sure the system is Windows 10 200...

Add a floating prompt for the header icon in the ElementUI table

This article mainly introduces how to add floatin...

The process of deploying a project to another host using Jenkins

environment Hostname ip address Serve Jenkins 192...

Detailed explanation of redo log and undo log in MySQL

The most important logs in the MySQL log system a...

Six ways to increase your website speed

1. Replace your .js library file address with the...

Solution to the paging error problem of MySQL one-to-many association query

The query data in the xml price inquiry contains ...

React's component collaborative use implementation

Table of contents Nesting Parent-child component ...

HTML set as homepage and add to favorites_Powernode Java Academy

How to implement the "Set as homepage" ...

Detailed explanation of Javascript basics

Table of contents variable Data Types Extension P...

Does Mysql ALTER TABLE lock the table when adding fields?

Table of contents Before MySQL 5.6 After MySQL 5....

How to view files in Docker image

How to view files in a docker image 1. If it is a...

How to install MySql in CentOS 8 and allow remote connections

Download and install. First check whether there i...