>

第八章 计时器(BEEPE凯雷德1)

- 编辑:澳门新葡亰平台游戏 -

第八章 计时器(BEEPE凯雷德1)

图片 1图片 2

效率如下:

天神的反应计时器里

 1 *-------------------------------------
 2     BEEPER1.C -- Timer Demo Program No.1
 3     (c) Charles Petzold, 1998
 4     -------------------------------------*/
 5 
 6 #include <Windows.h>
 7 
 8 #define ID_TIMER 1
 9 
10     LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
11 
12 int WINAPI WinMain( __in HINSTANCE hInstance
13     , __in_opt HINSTANCE hPrevInstance
14     , __in LPSTR lpCmdLine
15     , __in int nShowCmd )
16 {
17     static TCHAR szAppName[] = TEXT("Bepper1");
18     HWND hwnd;
19     MSG msg;
20     WNDCLASS wndclass;
21 
22     wndclass.style = CS_HREDRAW | CS_VREDRAW;
23     wndclass.lpfnWndProc = WndProc;
24     wndclass.cbClsExtra = 0;
25     wndclass.cbWndExtra = 0;
26     wndclass.hInstance = hInstance;
27     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
28     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
29     wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
30     wndclass.lpszMenuName = NULL;
31     wndclass.lpszClassName = szAppName;
32 
33     if (!RegisterClass(&wndclass))
34     {
35         MessageBox(NULL, TEXT("Program requires Windows NT!")
36             , szAppName, MB_ICONERROR);
37         return 0;
38     }
39 
40     hwnd = CreateWindow(szAppName, TEXT("Beeper1 Timer Demo")
41         , WS_OVERLAPPEDWINDOW
42         , CW_USEDEFAULT, CW_USEDEFAULT
43         , CW_USEDEFAULT, CW_USEDEFAULT
44         , NULL, NULL, hInstance, NULL);
45 
46     ShowWindow(hwnd, nShowCmd);
47     UpdateWindow(hwnd);
48 
49     while (GetMessage(&msg, NULL, 0, 0))
50     {
51         TranslateMessage(&msg);
52         DispatchMessage(&msg);
53     }
54 
55     return msg.wParam;
56 }
57 
58 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
59 {
60     static BOOL fFlipFlop = FALSE;
61     HBRUSH hBrush;
62     HDC hdc;
63     PAINTSTRUCT ps;
64     RECT rc;
65 
66     switch (message)
67     {
68     case WM_CREATE:
69         SetTimer(hwnd, ID_TIMER, 1000, NULL);
70         return 0;
71 
72     case WM_TIMER:
73         MessageBeep(-1);
74         fFlipFlop = !fFlipFlop;
75         InvalidateRect(hwnd, NULL, FALSE);
76         return 0;
77 
78     case WM_PAINT:
79         hdc = BeginPaint(hwnd, &ps);
80 
81         GetClientRect(hwnd, &rc);
82         hBrush = CreateSolidBrush(fFlipFlop ? RGB(255, 0, 0) : RGB(0, 0, 255));
83         FillRect(hdc, &rc, hBrush);
84 
85         EndPaint(hwnd, &ps);
86         DeleteObject(hBrush);
87         return 0;
88 
89     case WM_DESTROY:
90         KillTimer(hwnd, ID_TIMER);
91         PostQuitMessage(0);
92         return 0;
93     }
94 
95     return DefWindowProc(hwnd, message, wParam, lParam);
96 }

图片 3

笑笑和哭泣静静地

BEEPER1.C

 思路:

自由落体

 

  通过标签的data-time-end和data-time-start来收获倒计时的停止和起来时间(最初时间可传可不传)

祂试想着倒转的意义

  调用counter方法时需传入{callback:function(回调函数来接收状态和岁月间隔), countbeforeStart: Boolean(是不是需求开头倒计时,不传私下认可不须求起首倒计时)}

切!不还是

  假设有初叶时间,则始于时间必得低于甘休时间,不然弹出警告

笑笑和哭泣

  再判定早先时间是不是超越现在的时刻,如若过量则运动未初叶,

时光走得不假思索

  倘使急需开首倒计时则经过setInterval倒计时起来时间(每隔50ms计算叁回始发终结时间和现行反革命岁月的区间,当发掘间隔时间有变化则调用回调函数),当总括到运动始于时间低于或等于现在的时日时清空电火花计时器(clearInterval),然后再一次绑定click方法

  若是无需开头倒计时则调用回调函数,并用return停止推行后边的代码

  借使运动已开端则跟着判定活动完结时间是或不是超越今后的日子,倘使超越则通过setInterval倒计时得了时间(每隔50ms总结二遍活动甘休时间和当今岁月的间隔,当开掘间隔时间有生成则调用回调函数),当活动收尾时间低于或等于未来的年月时清空停车计时器并调用回调函数

  假设运动甘休时间低于或等于以后的时日,则运动完成,调用回调函数

  每一次调用回调函数都需传入state(-1为已终结,0为举办中,1为未初步),{day, hour, minute, second}(间隔日,时,分,秒)

  

亲自过问代码:

  CSS:

ul{width:400px; margin:auto; text-align:center;}
ul li{list-style:none; height:150px; border-radius:15px; box-shadow:0 5px 15px rgba(0,0,0,0.2); margin:30px; line-height:60px; background: lightblue;}

  HTML:

<ul>
    <li data-end-time="2020-10-31 14:41"></li>
    <li data-end-time="2025-11-01 12:40" data-start-time="2020-10-31 17:02"></li>
    <li data-end-time="2017-10-30 12:40" data-start-time="2017-10-28 16:58"></li>
</ul>
<script id="jquery_183" type="text/javascript" class="library" src="http://code.jquery.com/jquery-latest.jss"></script>

  JS:

;(function($){
    $.fn.counter=function(options){
        //获取日时分秒
        var getTimeSeperation = function(time){
            var day = Math.floor(time/(24*3600));
            var lessSecond = time-(day*24*3600);
            var hour = Math.floor(lessSecond/3600);
            lessSecond = lessSecond-hour*3600;
            var minute = Math.floor(lessSecond/60);
            var second = Math.floor(lessSecond-minute*60);
            return {
                day: day < 10 ? '0' + day : day,
                hour: hour < 10 ? '0' + hour : hour,
                minute: minute < 10 ? '0' + minute : minute,
                second: second < 10 ? '0' + second : second
            }
        }
        //获取时间对象
        var getTime = function(time){
            return time instanceof Date ? time.getTime() : new Date(time).getTime()
        }
        //获取距离现在的时间
        var getTimeFromNow = function(time){
                return Math.floor((time - new Date().getTime()) / 1000)
        }
        !options.callback && alert('请传回调函数!')
        this.each(function(){
            var self = this;
            this.endTime = $(this).attr('data-end-time');
            this.startTime = $(this).attr('data-start-time');
            this.callback = options.callback;
            if(!this.endTime){
                alert('请传入结束时间');
                return;
            }
            this.endTime = getTime(this.endTime);
            if(this.startTime){
                this.startTime = getTime(this.startTime);
                if(this.endTime < this.startTime){
                    alert('活动开始时间不能小于结束时间');
                    return;
                }
                this.timeFromNow = getTimeFromNow(this.startTime);
                this.newTimeFromNow = this.timeFromNow;
                if(this.timeFromNow > 0){
                    //活动未开始
                    if(options.countbeforeStart){
                        //需要倒数开始时间
                        this.interval = setInterval(function(){
                            self.newTimeFromNow = getTimeFromNow(self.startTime);
                            if(self.newTimeFromNow !== self.timeFromNow){
                                if(self.newTimeFromNow > 0){
                                    //活动即将开始
                                    self.callback(1, getTimeSeperation(self.newTimeFromNow));
                                    self.timeFromNow = self.newTimeFromNow;
                                }else{
                                    //活动已开始
                                    clearInterval(self.interval);
                                    $(self).counter({callback:self.callback})
                                }
                            }
                        },50)
                    }else{
                        this.callback(1, getTimeSeperation(this.timeFromNow))
                    }
                    return;
                }
            }    
            this.timeFromNow = getTimeFromNow(this.endTime);
            this.newTimeFromNow = this.timeFromNow;
            if(this.newTimeFromNow > 0){
                this.interval = setInterval(function(){
                    self.newTimeFromNow = getTimeFromNow(self.endTime);
                    if(self.newTimeFromNow !== self.timeFromNow){
                        if(self.newTimeFromNow > 0){
                            //活动进行中
                            self.callback(0, getTimeSeperation(self.newTimeFromNow));
                            self.timeFromNow = self.newTimeFromNow;
                        }else{
                            //活动已结束
                            clearInterval(self.counter);
                            self.callback(-1, getTimeSeperation(self.newTimeFromNow));
                        }
                    }
                },50)
            }else{
                //活动已结束
                this.callback(-1, getTimeSeperation(self.newTimeFromNow));
            }
        });
        return this;
    }
})(jQuery)

//调用方式
window.onload = function(){
    $('ul li').counter({
        callback: function(state, {day, hour, minute, second}){
            if(state === -1){
                //活动已结束
                this.innerHTML = '活动已结束';
            }else if(state === 1){
                //活动未开始
                var startTime = new Date($(this).attr('data-start-time')).toString();
                this.innerHTML = `活动将于${startTime}开始<br>距离活动开始还有${day}天${hour}时${minute}分${second}秒`;
            }else{
                //活动进行中
                this.innerHTML = `距离活动结束还有${day}天${hour}时${minute}分${second}秒`;
            }
        },
        countbeforeStart: true//是否需要开始倒计时(不传默认不需要开始倒计时)
    })
}

/****  公用方法  ****/
//日期格式转换    
Date.prototype.toString = function (f) {
    function tempfunc(opo, pos) {
        var val = ''; opo = String(opo);
        for (var a = 1; a < arguments.length; a++) {
            var chr = opo.charAt(arguments[a] - 1); val += chr;
        } return val;
    }
    if (!f) f = 'yyyy-MM-dd HH:mm:ss';
    var h12 = this.getHours() > 12 ? (this.getHours() - 12) : this.getHours();
    var tmp = {
        'yyyy': this.getFullYear(),
        'yy': tempfunc(this.getFullYear(), 3, 4),
        'MM': (this.getMonth() < 9 ? '0' : '') + (this.getMonth() + 1),
        'M': this.getMonth() + 1,
        'dd': (this.getDate() < 10 ? '0' : '') + this.getDate(),
        'd': this.getDate(),
        'hh': (h12 < 10 ? '0' : '') + h12, // 12小时制
        'h': h12,
        'HH': (this.getHours() < 10 ? '0' : '') + this.getHours(), // 24小时制
        'H': this.getHours(),
        'mm': (this.getMinutes() < 10 ? '0' : '') + this.getMinutes(),
        'm': this.getMinutes(),
        'ss': (this.getSeconds() < 10 ? '0' : '') + this.getSeconds(),
        's': this.getSeconds()
    };
    for (var p in tmp) f = f.replace(new RegExp('\b' + p + '\b', 'g'), tmp[p]);
    return f;
};

/****  公用方法  ****/

在线演示:

本文由操作系统发布,转载请注明来源:第八章 计时器(BEEPE凯雷德1)