Homemade Countdown Plugin

Source: Internet
Author: User
Tags current time require setinterval

Here we share a countdown plugin that I wrote, which uses Vue to encapsulate the component, and here's the code:

/
 * * Created by Mayouchen on 2017/4/13
 * youchen.ma@weimob.com *
 *

The entire file takes the idea of CMD and introduces Zepto, developed with Vue.

Define (function (Require, exports, module) {var $ = require ("Lib_cmd/zepto-cmd"), Vue = require ("lib_cmd/vue-

    CMD "); Countdown component Vue.component (' Countdown ', {Template: ' \ <div style= ' text-align:center;height:30 px;line-height:30px "> Current time \ <span v-html=" Day|fortime "></span> days <span v-html=" Hour|fortim E "></span> <span v-html=" minute|fortime "></span> min <span v-html=" Second|fortime "></  span> sec \ </div>\ ', props: ['], data:function () {var   self = this;
            Define days, hours, minutes, seconds var day=0, minute=0, hour = 0, second=0; return {day:day, hour:hour, Minute:minute, Second:se Cond}}, methods: {createtime:function () {//Calculate days, hours, minutes, seconds var time = new DaTe (). GetTime () setinterval (function () {time = time-1000;

                },1000);
                var delta = 1499508739691-time;
                    1499508739691 here is the cutoff time you want to pass in, here is the number of milliseconds, give a hypothetical value first if (Delta <=0) {return false;
                Console.log ("Countdown time has expired ~");
                    }else{var = this;
                    var day = Math.floor (delta/(24*60*60*1000)), d2ms = day*24*60*60*1000;
                    Self.day = day;
                    var hour = Math.floor ((delta-d2ms)/(60*60*1000)), h2ms = hour*60*60*1000;
                    Self.hour = hour;
                    var minute = Math.floor ((delta-d2ms-h2ms)/(60*1000)), m2ms = minute*60*1000;
                    Self.minute = minute;
    var second = Math.floor ((delta-d2ms-h2ms-m2ms)/1000), s2ms = second*1000;                Self.second = second;
            }}, Ready:function () {var = this;
            SetInterval (function () {self.createtime ();
        },1000); }, Filters: {//give single digit front complement 0 fortime:function (value) {if (typeof value = = ' undefined ')
                {return;
                } var html;
                var num_2 = value;
                if (Num_2 <) {num_2 = ' 0 ' + num_2;
            } return HTML = ' <span> ' +num_2+ ' </span> ';
    }

        }
    });
return Vue; });

If you want to separate the two digits of each time here, use the following filter:

    Filters: {//Data filter
            fortime:function (value) {
                if (typeof value = = ' undefined ') {
                    return;
                }
                var Html,arr;
                var num_2 = value;
                if (Num_2 <) {
                    num_2 = ' 0 ' + num_2;
                }
                arr = String (num_2). Split ("");
                return html = ' <span> ' +arr[0]+ ' </span> ' + ' <span> ' +arr[1]+ ' </span> ';
            }
        }

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.