jquery to achieve the countdown effect _jquery

Source: Internet
Author: User

Design a quiz game, each problem can have 20 seconds to answer, more than time to give a corresponding reminder, due to 20 seconds too long, not suitable for GIF dynamic map, the following look at the 5 seconds I write the test program results:

First, the main procedure

<! DOCTYPE html>
 
 

Second, CSS style

*{
  margin:0;
  padding:0;
}
html{
  font-size:12px
}
. countdown{
  Width:3.8rem;
  Text-align:center;
  Margin:2rem Auto 0 auto;
Countdown #countDownTime {
  font-size:2rem;
}


Three, jquery program

First, the principle of the countdown:

1. Convert time to 0:0 format

2, need to open a timer, every 1000ms let time automatically minus 1

3, to determine whether the time is 0, if 0 is to represent the end of the timer, at this time need to give hints or do other things

The following is a detailed implementation of the countdown program:

$ (function () {
  var countdowntime=parseint (5);    Here you set the answer for each question long
  function countdown (countdowntime) {
    var timer=setinterval (function () {
      if ( countdowntime>=0) {
        showTime (countdowntime);
        countdowntime--;
      } else{
        clearinterval (timer);
        Alert ("Timed over, give a hint");
      }
    },1000;
  }
  Countdown (countdowntime);
  function ShowTime (countdowntime) {      //This segment is the specific number of calculated minutes and seconds
    var minute=math.floor (COUNTDOWNTIME/60);
    var second=countdowntime-minute*60;
    $ ("#countDownTime"). Text (minute+ ": +second);
  }
}"

With the principle of my writing to see this JS program is easier to estimate, I hope to help small partners.

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.