Example of implementing timings by settimeout in JavaScript

Source: Internet
Author: User

JS Code

The code is as follows Copy Code

/**
* Countdown function
* Need to bind the button to the Click event
* such as: <input contenteditable=false value= send sms Type=button data-cke-pa-onclick= "setinterval (' CountDown"), 1000); "data-cke-editable=" 1 ">
* 30 represents the number of seconds, the number of seconds to be counted can be changed
*/
function Countdown (Obj,second) {
If the number of seconds is still greater than 0, it means the countdown is not over yet.
if (second>=0) {
Get the text on the default button
if (typeof buttondefaultvalue = = ' undefined ') {
Buttondefaultvalue = Obj.defaultvalue;
}
button to non-clickable state
Obj.disabled = true;
The contents of the button render the countdown status
Obj.value = Buttondefaultvalue+ ' (' +second+ ') ';
Time Minus One
second--;
Repeat execution after one second
settimeout (function () {countdown (Obj,second);},1000);
Otherwise, the button resets to the initial state
}else{
button not to click state
obj.disabled = false;
The content in the button is restored to its original state
Obj.value = Buttondefaultvalue;
}
}

Html

Code

  code is as follows copy code

    JS Button 30-second countdown effect <p>
    <input onclic K= "Countdown (this,30);" type= "button" value= "Send SMS"/></p>
  & nbsp June blog qttc.net <p>
    in the button-bound JS Click event &lsquo;30&rsquo; represents the number of seconds countdown, can set the number of seconds in the binding, flexible application. Of course, this countdown is just a front-end control, if the need for a more secure approach should be the server side also make judgments. </p>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.