Use JS to display the current time of the system and implement the Countdown function and trigger the modal box (mask) function

Source: Internet
Author: User
Tags setinterval

Often in our web pages need a countdown to trigger some functions, such as masks, in this project, by using jquery,bootstrap, to achieve the display system current time, and the implementation of the Countdown function, the countdown will pop up the modal box (mask layer). The modal frame interface is friendly and flexible, appearing as a pop-up dialog box. See 1 for Details:

Project source file Address: Https://github.com/zhangxy1035/Countdown

Reference: http://v3.bootcss.com/javascript/#modals

Figure 1:

About the countdown function is as Follows:

1  varTime = 10*1000;//calculate the number of milliseconds2     varse;3SE = setinterval (' t_time () ', 1000);4     functiont_time () {5         //Countdown6         varMM = parseint (time/60/1000%60,10);//number of minutes left7         varSS = parseint (time/1000%60,10);//number of seconds remaining8MM =checktime (mm);9SS =checktime (ss);Ten$ ("#timer"). HTML (mm+ ' +ss+ ' seconds '); oneTime = time-1000; a         if(mm==0 && ss==0) { -$ ("#myEnd"). Modal ("toggle")); - clearinterval (se); the         } -     } -  -     functionchecktime (i) { +         if(i<10) { -i = "0" +i; +         } a         returni; at}

In the above code, the Checktime function, the main function is to control the display, for example, the distance time to the end of 8 seconds, the system will be displayed as 08.

Code references in Html:

1  <span class= "label badge-success" > Current time: &nbsp;<span id= "current-time" ></span></span>

The Modal box (mask layer) code is:

1 div id= "myend" class= "modal hide" > 2     <div class= "modal-header" > 3         < Button data-dismiss= "modal" class= "close" type= "button" >x</button> 4         5     </div> 6     <div class= "modal-body" > 7         <p> You have run out of time </p> 8      </div> 9     <div class= "modal-footer" >         <a data-dismiss= "modal" class= "btn btn-success" href= "#" > OK </a>     </div> </div>

After the modal box appears, you can jump to the page you're linking To.

Show current system time:

1    // Show Current Time 2     SetInterval (function() {3         var now = (new  Date ()). toLocaleString (); 4         $ (' #current-time '). text (now); 5     }, 1000);

Use JS to display the current time of the system and implement the Countdown function and trigger the modal box (mask) function

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.